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
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Get – set pixels and RGB Values of image in VB.net - C#.net.

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

In this simple article you will learn that how we can get and set pixels values and RGB values of 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 or set 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.

Get Pixel:

To get a specific pixel value of an image, for this purpose image class provides us a function named getpixel(). This function takes two integers as x, y coordinate of a pixel and returns an object of color class. This object contains the information about pixel.

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

Now write the following code on Button click event:

C#

private void btn_getPixels_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            listBox1.Items.Add("Pixel             Color");

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

                        listBox1.Items.Add(i.ToString() + "," + j.ToString() + "             " + c.Name);

                    }

                }

                MessageBox.Show("SUCESSFULLY DONE");

            }

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

        }

VB

Private Sub btn_getPixels_Click(ByVal sender As ObjectByVal As EventArgs)
        listBox1.Items.Clear
        listBox1.Items.Add(
"Pixel             Color")
        
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)
                    listBox1.Items.Add((i.ToString + (
","  _
                                    + (j.ToString + (
"             " + c.Name)))))
                    j 
(j + 1)
                
Loop
                
(i + 1)
            
Loop
            
MessageBox.Show("SUCESSFULLY DONE")
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get pixel value.

Set Pixel:

To set a specific pixel value of an image, for this purpose image class provides us a function named setpixel(). This function takes two integers as x, y coordinate of a pixel and color value as a parameter.

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_setpixel_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++)

                    {

                        img.SetPixel(i,j,Color.Fuchsia);

                    }

                }

                pictureBox1.Image = img;

                MessageBox.Show("SUCESSFULLY DONE");

            }

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

        }

VB

Private Sub btn_setpixel_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)
                    img.SetPixel(i, j, Color.Fuchsia)
                    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 set pixel value.

Get RGB Values:

To get the RGB values of a pixel first we have to get a pixel value by using the method as I mentioned above. After that by using the color class properties we can get RGB values. To get red we have a property named R. This property returns a byte value. Same for blue and green values we have B and G property.

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

Now write the following code on Button click event:

C#

private void btn_getRGB_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            listBox1.Items.Add("Pixel             R     G     B");

            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 = Convert.ToInt16(c.R);

                        int g = Convert.ToInt16(c.G);

                        int b = Convert.ToInt16(c.B);

                        listBox1.Items.Add(i.ToString() + "," + j.ToString() + "             " + r.ToString() + "     " + g.ToString() + "     " + b.ToString());

                    }

                }

                MessageBox.Show("SUCESSFULLY DONE");

            }

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

        }

VB

 Private Sub btn_getRGB_Click(ByVal sender As ObjectByVal As EventArgs)
        listBox1.Items.Clear
        listBox1.Items.Add(
"Pixel             R     G     B")
        
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 = Convert.ToInt16(c.R)
                    
Dim As Integer = Convert.ToInt16(c.G)
                    
Dim As Integer = Convert.ToInt16(c.B)
                    listBox1.Items.Add((i.ToString + (
","  _
                                    + (j.ToString + (
"             "  _
                                    + (r.ToString + (
"     "  _
                                    + (g.ToString + (
"     " + b.ToString)))))))))
                    j 
(j + 1)
                
Loop
                
(i + 1)
            
Loop
            
MessageBox.Show("SUCESSFULLY DONE")
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get RGB values.

Set Pixel:

To set a RGB values of a pixel of an image, for this purpose image class provides us a function named setpixel(). This function takes two integers as x, y coordinate of a pixel and color value as a parameter. We can pass our RGB values in static function of color class named fromargb(). This function takes RGB values as parameter and returns color value. This color value is passed in setpixel() function. By using this technique we can change RGB values of a 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_setrgb_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                Color c;

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

                {

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

                    {

                        c = img.GetPixel(i, j);

                        int r = Convert.ToInt16(c.R);

                        int g = Convert.ToInt16(c.G);

                        int b = Convert.ToInt16(c.B);

                        r = 255;

                        g = 255;

                        b = 0;

                        img.SetPixel(i,j,Color.FromArgb(r,g,b));

                    }

                }

                MessageBox.Show("SUCESSFULLY DONE");

                pictureBox1.Image = img;

            }

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

        }

VB

Private Sub btn_setrgb_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 / 
2))
                
Dim As Integer = 0
                
Do While (j  _
                            < (img.Height / 
2))
                    c 
img.GetPixel(i, j)
                    
Dim As Integer = Convert.ToInt16(c.R)
                    
Dim As Integer = Convert.ToInt16(c.G)
                    
Dim As Integer = Convert.ToInt16(c.B)
                    r 
255
                    
255
                    
0
                    
img.SetPixel(i, j, Color.FromArgb(r, g, b))
                    j 
(j + 1)
                
Loop
                
(i + 1)
            
Loop
            
MessageBox.Show("SUCESSFULLY DONE")
            pictureBox1.Image 
img
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to set RGB values.

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 and set pixels values and RGB values of image in VB and C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Join - Interrupt threads and get managed thread id using Thread class in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2014 DevASP.net