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
 

Convert colored image into gray scale image in VB.net - C#.net.

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

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

 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:

C#

private void btn_grayscale_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 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); }

        }

VB

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

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

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

Disclaimer - Privacy
© 2002-2017 DevASP.net