14,421,761 members
Rate this:
See more:
Dear friends,,

I have an image in a picture box, and I want to get Pixels(or all the area that contains) of all shades of blue color in that picture or image. For Example: I have a picture box with an image and two buttons, one for blue and on for green, I want when I click on blue then only blue shades in that picture should be visible and the rest is not visible. Same for the green button.

Please friends help me.

Thanks in advance!
Posted
Updated 21-May-12 5:31am
v4

Rate this:

## Solution 1

I'm not sure if you mean the same thing as I when talking of a shade of a color: See here please[^].
Could it be that you really want to calculate the hue part of HSV[^] and all colors that fall into a certain hue range are considered to be in a class?

Regards,

Manfred
Comments
MuhtarQong 21-May-12 10:44am

I assume that he is going to split an RGB image to Blue, Green and Red components (layer or band). Then ,display only a specific layer (for example Blue layer only) each time a button (corresponding buttuns to colors) is clicked.
Hopefully, I will provide a solution for your question during the lunch-break.
BobJanova 21-May-12 11:02am

The use of 'blue' and 'green' leads me to believe he's talking about channels as well, but it could also be a hue filter.
rahulbhadouria 21-May-12 13:27pm

friends I have a Image with multiple colors but I want all shades of particular color...i may be green or red or blue.
rahulbhadouria 21-May-12 13:35pm

Dear Manfred,,

You are guessing right,,I want exactly what you are talking..
Rate this:

## Solution 2

I have made function and enum. For convinience I have created an enum for layer selection, "WhichLayer"
```public enum WhichLayer
{
Red=0,
Green=1,
Blue=2
}```

Then, the functions as follows:
```private Bitmap GetSpecificLayer(Bitmap image, WhichLayer layer)
{
Rectangle rect=new Rectangle(0, 0, image.Width, image.Height);
BitmapData dataIn = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

Bitmap result = new Bitmap(rect.Width,rect.Height,PixelFormat.Format8bppIndexed);
// Keep the original resolotion
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Set Grayscale palette
SetGrayscalePalette(result);
BitmapData dataOut = result.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

int strideIn = dataIn.Stride;
int strideOut = dataOut.Stride;
int lineOffsetIn = dataIn.Stride - rect.Width * 3;
int lineOffsetOut = dataOut.Stride - rect.Width;

// Get layer index
int ind=0;
switch(layer)
{
case WhichLayer.Blue: ind=0; break;
case WhichLayer.Green:ind=1; break;
case WhichLayer.Red: ind = 2;break;
}
unsafe
{
byte* pIn = (byte*)(void*)dataIn.Scan0;
byte* pOut = (byte*)(void*)dataOut.Scan0;

for (int y = 0; y < image.Height; y++)
{
for (int x = 0; x < image.Width; x++)
{
pOut[0] = pIn[ind];
pIn += 3;       // skip RGB24=3; RGB32=4...
pOut++;
}
pIn += lineOffsetIn;
pOut += lineOffsetOut;
}
}
image.UnlockBits(dataIn);
result.UnlockBits(dataOut);

return result;
}

private static void SetGrayscalePalette(Bitmap b)
{
ColorPalette pal = b.Palette;
for (int i = 0; i < 256; i++)
pal.Entries[i] = Color.FromArgb(255, i, i, i);
b.Palette = pal;
}
```

The above is only for RGB images (24-bit). If you need for all RGB color images you may do separately for each format. Or you may use third party tools. You may find one here:http://www.artuxsoft.com[^]
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 170 Wendelius 160 0x01AA 60 Thomas Daniels 55 phil.o 50
 OriginalGriff 4,083 Richard MacCutchan 1,401 phil.o 820 Wendelius 810 Patrice T 784

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100