Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to take a Negative of an image in VB.net - C#.net.

Author: Shehzad Hemani
Download Source Code : 1067_source.zip

In this simple article you will learn that how we can get negative of an image in VB and C#.net.

 Negative of Image:

Negative of an image means to take the opposite of the image in sense of colors or how the image will look in camera negative. It looks like an image in the camera negative.

Now the question is how we can convert image in its negative. To do this we change the pixel values of the image. Image is consisting of pixels and each pixel has RGB values. Which means each pixel is made of red green and blue colors. By combining these colors we can get a new color. Each color has its value or intensity. These values lie between 0 to 255.

To take the negative of image we get a pixel subtract each RGB value from 255 and store them again on the same pixel. We can get pixel by specifying row and column because image is consisting of pixels those are in form of a matrix. Matrix size is the image height * image width.

First we will get each pixel one by one, change its value according to formula specified above and then store them again on the same locations.

To demonstrate make a window application. Drag one button and a picture box on form.

Now write the following code on Button click event:

C#

private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                Color c;

                for (int i = 0; i < img.Width; i++)

                {

                    for (int j = 0; j < img.Height; j++)

                    {

                        c = img.GetPixel(i, j);

                        int r = 0;

                        r = Convert.ToInt16(c.R);

                        int g = 0;

                        g = Convert.ToInt16(c.G);

                        int b = 0;

                        b = Convert.ToInt16(c.B);

                        int newr = 0;

                        newr = 255 - r;

                        int newg = 0;

                        newg = 255 - g;

                        int newb = 0;

                        newb = 255 - b;

                        c = Color.FromArgb(newr, newg, newb);

                        img.SetPixel(i, j, c);

                    }

                }

                pictureBox1.Image = img;

                MessageBox.Show("SUCESSFULLY DONE");

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub button1_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim As Color
            
Dim As Integer = 0
            
Do While (i < img.Width)
                
Dim As Integer = 0
                
Do While (j < img.Height)
                    c 
img.GetPixel(i, j)
                    
Dim As Integer = 0
                    
Convert.ToInt16(c.R)
                    
Dim As Integer = 0
                    
Convert.ToInt16(c.G)
                    
Dim As Integer = 0
                    
Convert.ToInt16(c.B)
                    
Dim newr As Integer = 0
                    
newr (255 - r)
                    
Dim newg As Integer = 0
                    
newg (255 - g)
                    
Dim newb As Integer = 0
                    
newb (255 - b)
                    c 
Color.FromArgb(newr, newg, newb)
                    img.SetPixel(i, j, c)
                    j 
(j + 1)
                
Loop
                
(i + 1)
            
Loop
            
pictureBox1.Image img
            MessageBox.Show(
"SUCESSFULLY DONE")
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to take negative of an image.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

            this.Text = "DEVASP IMAGE APPLICATION";

        }

VB

 Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP IMAGE APPLICATION"
    
End Sub

This simple article tells that how we can get negative of an image in VB and C#.net.

Article Comments
thank you very much, helped me

Posted on 8/7/2010 3:43:39 PM by JOSUA

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Convert colored image into gray scale image in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net