Image processing libraries are
not wildly used as in C or in Matlab. Although C# has some functionalities that
facilitates image processing programming, but it needs to be well organized.
Through out the next few weeks, we will build together our general use image
processing class in C#.
Before We Start
First we need to start a windows forms application.
Second Place a picturebox, and set a default image to any picture you want.
Getting A Color Component
Separating the three main components colors is very easy in C#, so if you want
to get a picture with only the component you could use the following code
Bitmap RedImage =
new Bitmap(pictureBox1.Image);
for (int
i = 0; i < RedImage.Height; i++)
for (int
j = 0; j < RedImage.Width; j++)
{
Color pixel =
RedImage.GetPixel(j, i);
RedImage.SetPixel(j, i, Color.FromArgb(pixel.R,
0, 0));
}
return RedImage;
You can make a slight change to the function in order to get the Blue and Green components.
// Green Component
public
Bitmap GetGreenComponent()
{
Bitmap GreenImage =
new Bitmap(pictureBox1.Image);
for (int
i = 0; i < GreenImage.Height; i++)
for (int
j = 0; j < GreenImage.Width; j++)
{
Color pixel =
GreenImage.GetPixel(j, i);
GreenImage.SetPixel(j, i,
Color.FromArgb(0, pixel.G, 0));
}
return GreenImage;
}
public
Bitmap GetBlueComponent()
{
Bitmap BlueImage =
new Bitmap(pictureBox1.Image);
for (int
i = 0; i < BlueImage.Height; i++)
for (int
j = 0; j < BlueImage.Width; j++)
{
Color pixel =
BlueImage.GetPixel(j, i);
BlueImage.SetPixel(j, i,
Color.FromArgb(0, 0, pixel.B));
}
return BlueImage;
}
Converting An Image From Colored (RGB) to Gray
We can convert the image color scale from RGB to Gray scale, by getting the
average value of each color component in a pixel, and placing the new value in
each component.
public Bitmap
ConvertToGray()
{
Bitmap GrayImage =
new Bitmap(pictureBox1.Image);
for (int
i = 0; i < GrayImage.Height; i++)
for (int
j = 0; j < GrayImage.Width; j++)
{
Color pixel =
GrayImage.GetPixel(j, i);
int Gray = (pixel.R +
pixel.G + pixel.B) / 3;
GrayImage.SetPixel(j, i, Color.FromArgb(Gray,
Gray, Gray));
}
return GrayImage;
}
In the next article we will write a program to detect a certain color, with a certain color components ranges.

Sonu ChaudharyPosted Feb 25, 2016, 7:18 AM
nice article
Grabsi AminePosted Nov 17, 2012, 1:48 AM
nice ^^ i use Matlab in image processing cause it is the most simple :) ,, thank you a lot this is amazing expl
ethen jamesPosted Mar 25, 2012, 7:41 AM
I have been searching image processing articles all over the Internet but your was the easiest and the best explained one. Thank you so much!!
Yao ZiggahPosted Nov 13, 2011, 10:34 PM
Can anyone help me out with an algorithm in Image Processing using Sobel,SUSAN and SNAKE algorithm in c#
amirhossein kalantariPosted Feb 14, 2011, 2:40 AM
Thank You...
ArashPosted Feb 2, 2011, 8:17 AM
hi dear Tamer i'm ali and today i strart to work with C# image processing. now i want to just open a bitmap file and show it. how can i do? so thanks.
Fatima AsifPosted Dec 20, 2010, 4:08 PM
Can i use this technique as i am using A forge.net framework and doing my course project
Mike GoldeditedPosted Jun 10, 2010, 11:18 AMEdited Jun 11, 2010, 4:56 PM
Good topic and nicely done. I would add an upload to the article. Also, you may want to put a bio in your profile so people know who you are in the community. Looking forward to the next article on image processing. -Mike Gold C# Corner Editor
nazib abdullahPosted Jun 2, 2010, 7:25 AM
carry on the next
Abhishek ShuklaPosted Dec 5, 2009, 3:14 PM
Where is the code for gray scale?