SIGN UP MEMBER LOGIN:    
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
share 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
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor