private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog OpenDiag = new OpenFileDialog();
OpenDiag.Filter = "Bitmap|*.bmp|JPeg|*.jpeg|JPeg|*.jpg|Portable Network Graphics|*.png";
if (OpenDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBoxOriginal.Image = Image.FromFile(OpenDiag.FileName);
pictureBoxOriginal.BringToFront();
}
}
private enum RGB
{
/// <summary>
/// Escala de vermelho
/// </summary>
Red,
/// <summary>
/// Escala de verde
/// </summary>
Green,
/// <summary>
/// Escala de azul
/// </summary>
Blue,
/// <summary>
/// Escala de cinza
/// </summary>
Gray
}
/// <summary>
/// Separates a specific color of an image from another or take 'em all without erasing the image
/// </summary>
/// <param name="Original">The image to decompose</param>
/// <param name="WhichColor">Color to separate</param>
/// <returns>The image only in a color tone or grayscale</returns>
private Image Decompose(Bitmap Original, RGB WhichColor)
{
//return Decomposed;
}
That's right, it must return an image.
//This is the bitmap we will create and futurely, convert to Image (by casting) and return.
Bitmap BDecomposed = new Bitmap(Original.Width, Original.Height);
//Also create an integer that will be used by one of our loops.
int X = 0;
Now the loops.
while (X < Original.Width)
{
int Y = 0; //This variable will be used by the "inner" loop.
//The inner loop
X++;
}
So, as far we have this in our "Decompose" Method:
private Image Decompose(Bitmap Original, RGB WhichColor)
{
Bitmap BDecomposed = new Bitmap(Original.Width, Original.Height);
int X = 0;
while (X < Original.Width)
{
int Y = 0; //This variable will be used by the "inner" loop.
//The inner loop
X++;
//return Decomposed;
}
}
int Ro = Original.GetPixel(X, Y).R;
int Go = Original.GetPixel(X, Y).G;
int Bo = Original.GetPixel(X, Y).B;
int Rn = 0;
int Gn = 0;
int Bn = 0;
The
Xo variable are the Red, Green and Blue values of the original pixel in that position and the
Xn variables are the Red, Green and Blue values for our new pixel color.
if (WhichColor == RGB.Red)
{
Rn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Green)
{
Gn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Blue)
{
Bn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Gray)
{
Rn = (Ro + Go + Bo) / 3;
Gn = (Ro + Go + Bo) / 3;
Bn = (Ro + Go + Bo) / 3;
}
Let's understand it.
// The variable used by the inner loop that is set in the outer loop
int Y = 0;
while (Y < Original.Height)
{
int Ro = Original.GetPixel(X, Y).R;
int Go = Original.GetPixel(X, Y).G;
int Bo = Original.GetPixel(X, Y).B;
int Rn = 0;
int Gn = 0;
int Bn = 0;
if (WhichColor == RGB.Red)
{
Rn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Green)
{
Gn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Blue)
{
Bn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Gray)
{
Rn = (Ro + Go + Bo) / 3;
Gn = (Ro + Go + Bo) / 3;
Bn = (Ro + Go + Bo) / 3;
}
}
Now that we've got the color, it's easy: Just set the color of the pixel we're currently working on in our new image to the color we've got:
Color NewTone = Color.FromArgb(Rn, Gn, Bn);
BDecomposed.SetPixel(X, Y, GrayTone);
//This is to set the loop to work on the next pixel in the coordinate Y
Y++;
So, the whole
Decompose method looks like this:
private Image Decompose(Bitmap Original, RGB WhichColor)
{
Bitmap BDecomposed = new Bitmap(Original.Width, Original.Height);
//We will set it to work on the pixels with the X coordinate 0, that means, the first column of pixels in the image.
int X = 0;
while (X < Original.Width)
{
//Everytime the outer loop restarts, this will set the inner loop to start from the top of the column of pixels.
int Y = 0;
while (Y < Original.Height)
{
int Ro = Original.GetPixel(X, Y).R;
int Go = Original.GetPixel(X, Y).G;
int Bo = Original.GetPixel(X, Y).B;
int Rn = 0;
int Gn = 0;
int Bn = 0;
if (WhichColor == RGB.Red)
{
Rn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Green)
{
Gn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Blue)
{
Bn = (Ro + Go + Bo) / 3;
}
else if (WhichColor == RGB.Gray)
{
Rn = (Ro + Go + Bo) / 3;
Gn = (Ro + Go + Bo) / 3;
Bn = (Ro + Go + Bo) / 3;
}
Color GrayTone = Color.FromArgb(Rn, Gn, Bn);
BDecomposed.SetPixel(X, Y, GrayTone);
//This sets the inner loop to work with the next pixel in that column
Y++;
}
//This will pass the current column of pixels being worked on by the inner loop, to the next column in the image.
X++;
}
//This converts our created Bitmap to an Image
Image Decomposed = (Image)BDecomposed;
return Decomposed;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBoxGrayScale.Image = Decompose((Bitmap)pictureBoxOriginal.Image, RGB.Gray);
pictureBoxGrayScale.BringToFront();
pictureBoxRed.Image = Decompose((Bitmap)pictureBoxOriginal.Image, RGB.Red);
pictureBoxRed.BringToFront();
pictureBoxGreen.Image = Decompose((Bitmap)pictureBoxOriginal.Image, RGB.Green);
pictureBoxGreen.BringToFront();
pictureBoxBlue.Image = Decompose((Bitmap)pictureBoxOriginal.Image, RGB.Blue);
pictureBoxBlue.BringToFront();
}
NOTE: The "BringToFront" is being called so the Labels won't be ahead the picture box, after its image is set.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
Just a suggestion : )
Commented: