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

Convert colored image into gray scale image in -

Author: Shehzad Hemani
Download Source Code :

In this simple article you will learn that how we can convert colored image into gray scale image in VB and

 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.

We can get the pixel by specifying the x and y coordinate of the pixel because image pixels are in the form of matrix. Matrix size is equal to image height * image width.

To convert the image in gray scale first gets RGB value of a pixel. Calculate the maximum value from the RGB values. Assign that maximum value to R, G and B.  Do this process for every pixel.

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 btn_grayscale_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 max1 = System.Math.Max(r, g);

                        int final = System.Math.Max(max1, b);

                        r = final;

                        g = final;

                        b = final;

                        c = Color.FromArgb(r, g, b);

                        img.SetPixel(i, j, c);



                pictureBox1.Image = img;

                MessageBox.Show("SUCESSFULLY DONE");


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



Private Sub btn_grayscale_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 max1 As Integer = System.Math.Max(r, g)
Dim final As Integer = System.Math.Max(max1, b)
Color.FromArgb(r, g, b)
                    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 convert image in gray scale.

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 convert colored image into gray scale image in VB and

Add Article Comment:
Name :
Email Address :
Comments :
<< Convert colored image into black and white image in -

Disclaimer - Privacy
© 2002-2017