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 black and white image in VB.net - C#.net.

Author: Maliha Atteeq
Download Source Code : 1065_source.zip

In this simple article you will learn that how we can convert colored image into black and white 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. This is in the case of colored image. In black and white image every pixel is contains one color black or white.

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 black and white first gets RGB value of a pixel. Calculate the sum of these values and take average. Like (R+G+B)/3. After calculating the average check if the value is greater than 128 then make the pixel white by assigning the 255 to each RGB value else assign the 0 to each RGB value to make it black. 255 is white color and 0 is black color.  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_blacknwhite_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 ans = (r + g + b) / 3;

                        if (ans > 128)

                        {

                            r = 255;

                            g = 255;

                            b = 255;

                        }

                        else

                        {

                            r = 0;

                            g = 0;

                            b = 0;

                        }

                        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_blacknwhite_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 ans As Integer = ((r  _
                                + (g + b))  _
                                / 
3)
                    
If (ans > 128Then
                        
255
                        
255
                        
255
                    
Else
                        
0
                        
0
                        
0
                    
End If
                    
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 black and white.

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 black and white image in VB and C#.net.

Article Comments
I need ASP.Net code for Converting Color image to black&white image using C#.net

Posted on 8/19/2010 3:24:35 AM by K.L.P.Naidu

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Solution of coin change problem by using greedy algorithm in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net