Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116

    [vb.net-c#-framework 3.5-4] rendere negativa una immagine

    codice:
    public static void ImgConvertNegativeMatrix(System.Drawing.Bitmap bitmap)
    {
    	System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
    	//Dim matrix As New System.Drawing.Imaging.ColorMatrix()
    
    	//matrix(0, 0) = -1
    	//matrix(1, 1) = -1
    	//matrix(2, 2) = -1
    
    	float[][] values = {
    		new float[] {-1, 0, 0, 0, 0},
    		new float[] {0, -1, 0, 0, 0},
    		new float[] {0, 0, -1, 0, 0},
    		new float[] {0, 0, 0, 1, 0},
    		new float[] {0, 0, 0, 0, 1}};
    
    	//usa la matrice per inizializzare un oggetto colorMatrix
    	System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(values);
    
    	System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
    	attributes.SetColorMatrix(matrix);
    
    	System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
    	graphics.DrawImage(bitmap, bounds, 0, 0, bitmap.Width, bitmap.Height, System.Drawing.GraphicsUnit.Pixel, attributes);
    
    	graphics.Dispose();
    
    
    }
    uso questo codice per rendere negativa una immagine. L'unica cosa che combino è però trasformare l'immagine in un rettangolo completamente nero :master:

    Dove sbaglio?
    Pietro

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Qui:

    La tua sub diventa una funzione che ritorna un'oggetto graphics.

    [code]
    public Graphics ImgConvertNegativeMatrix(System.Drawing.Bitmap bitmap)
    {
    System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
    float[][] values = { new float[] { -1, 0, 0, 0, 0 }, new float[] { 0, -1, 0, 0, 0 }, new float[] { 0, 0, -1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } };

    System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(values);

    System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
    attributes.SetColorMatrix(matrix);

    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
    graphics.DrawImage(bitmap, bounds, 0, 0, bitmap.Width, bitmap.Height, System.Drawing.GraphicsUnit.Pixel, attributes);
    return graphics;
    graphics.Dispose();
    }
    [code]

    Po la applichi:

    codice:
      private void button1_Click(object sender, EventArgs e)
    {
        Graphics g = ImgConvertNegativeMatrix(PictureBox1.Image);
        Graphics g2 = PictureBox2.CreateGraphics;
        g2.DrawImage(PictureBox1.Image, 0, 0);
    }
    
     :ciauz:
    Sbagliare è umano, perseverare è diabolico.

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Ti ringrazio della risposta.
    Purtroppo non ho risolto il problema. Sia il tuo codice sia il mio funziona perfettamente con windows Xp, ma non con windows 7 (con vista non ho provato)

    Questo il codice di prova
    codice:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    
    
    
    namespace a
    {
        class Program
        {
           static void Main(string[] args)
            {
                using(Bitmap bmp = new Bitmap(@"c:\tmp\prova.jpg"))
                {
                    Graphics g = ImgConvertNegativeMatrix(bmp);
                    Graphics g2 = Graphics.FromImage(bmp);
                    g2.DrawImage(bmp, 0, 0);
    
                    
                    bmp.Save(@"c:\tmp\prova1.jpg");
                }
            }
    
    
           static public Graphics ImgConvertNegativeMatrix(System.Drawing.Bitmap bitmap)
            {
                System.Drawing.Rectangle bounds = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
                float[][] values = { new float[] { -1, 0, 0, 0, 0 }, new float[] { 0, -1, 0, 0, 0 }, new float[] { 0, 0, -1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 0, 0, 0, 0, 1 } };
    
                System.Drawing.Imaging.ColorMatrix matrix = new System.Drawing.Imaging.ColorMatrix(values);
    
                System.Drawing.Imaging.ImageAttributes attributes = new System.Drawing.Imaging.ImageAttributes();
                attributes.SetColorMatrix(matrix);
    
                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
                graphics.DrawImage(bitmap, bounds, 0, 0, bitmap.Width, bitmap.Height, System.Drawing.GraphicsUnit.Pixel, attributes);
                return graphics;
                graphics.Dispose();
            }
    
    
    
    
    
        }
    }
    Pietro

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    In effetti è vero.
    Cosi dovrebbe andare, modificando questa linea.

    codice:
      float[][] values = { new float[] { -1, 0, 0, 0, 0 }, new float[] { 0, -1, 0, 0, 0 }, new float[] { 0, 0, -1, 0, 0 }, new float[] { 0, 0, 0, 1, 0 }, new float[] { 1, 1, 1, 0, 1 } };
    Sbagliare è umano, perseverare è diabolico.

  5. #5
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    insomma, è bastato cambiare dei 0 in 1 per fare funzionare tutto

    Adesso va sia in xp sia in windows 7

    ciao e grazie ancora
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.