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

How to take a Negative of an image in -

Author: Shehzad Hemani
Download Source Code :

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

 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:


private void button1_Click(object sender, EventArgs e)




                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); }



Private Sub button1_Click(ByVal sender As ObjectByVal As EventArgs)
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)
img.GetPixel(i, j)
Dim As Integer = 0
Dim As Integer = 0
Dim As Integer = 0
Dim newr As Integer = 0
newr (255 - r)
Dim newg As Integer = 0
newg (255 - g)
Dim newb As Integer = 0
newb (255 - b)
Color.FromArgb(newr, newg, newb)
                    img.SetPixel(i, j, c)
(j + 1)
(i + 1)
pictureBox1.Image img
Catch ex As Exception
End Try
    End Sub

This is simple code to take negative of an image.

Now write the following code on FORM LOAD event:


private void Form1_Load(object sender, EventArgs e)


            this.Text = "DEVASP IMAGE APPLICATION";



 Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
End Sub

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

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 -

Disclaimer - Privacy
© 2002-2017