ARTICLE

C# Image Proccessing Techniques: Part II

Posted by Tamer Khalil Articles | Algorithms in C# June 11, 2010
In the series of article we will build together our general use image processing class in C#.
Reader Level:
Download Files:
 

In my first article I discussed how to split the three color components of an image using C#, in addition to converting the colors from RGB to Gray Scale. In this article We will build our color selector that gets the coordinates of the point inside the image, and the 3 three color components of the point.


Lets start with creating a new window forms project, then place a picture box and place 8 labels in the main form. 4 labels for the items for the data headers, and 4 for the data display. Select an image to be loaded in the picture box.

right Click on the picture box and in then select properties then the events, and double click on "MouseMove" this will make an event when a mouse moves over the picture box.

Notice the function created 
 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

MouseEventArgs e holds attributes concerning the Mouse Position in the image box.

Put this part a side, and lets the function that will display the color coordinates, given the coordinates of the selected point. Simply this function takes image from the picture box, and gets a the given point from the extracted image, then displays the coordinates and the color components in the labels.

Here is my code:

 public void PrintColorComponents(int x,int y)
{
Bitmap image = new Bitmap(pictureBox1.Image);
lbCoordinates.Text = x.ToString() + "," + y.ToString();
Color color = image.GetPixel(x, y);
lbRed.Text = color.R.ToString();
lbGreen.Text = color.G.ToString();
lbBlue.Text = color.B.ToString();
}

Lets go back to the Mouse Move event function. Call inside it the function PrintColorComponents and give it the following parameters, e.X, and e.Y which are the location of the cursor inside the picture box.
  private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
PrintColorComponents(e.X,e.Y);
}

Then Run the program; Here is a screen shot of mine.
Articel2-ScreenShot.jpg
()

Attached is the sample program.

In the next article we will learn how to change the background of an image, for instance replacing the white background of my picture with evil tower, as if I took it in Paris!!!

Feel free to contact me.

Login to add your contents and source code to this article
post comment
     

if i want to change image on run time then wil it work on that image?

Posted by Sharjeel Qadeer Feb 28, 2011

this error: Parameter must be positive and < Width.
Parameter name: x

Posted by hai nam ngo Oct 10, 2010

Can you send the error?

Posted by Tamer Khalil Oct 10, 2010

i was tried your code but there is one error

Posted by hai nam ngo Oct 10, 2010

This is a nice free utility for determining color components.  Good article.  One thing you might want to add is a way to toggle between hex and decimal using
String.Format( "{0:x}", color.R);

Posted by Mike Gold Jun 13, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts