Click here to Skip to main content
14,421,761 members
Rate this:
Please Sign up or sign in to vote.
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!
Updated 21-May-12 5:31am
Rate this:
Please Sign up or sign in to vote.

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?


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:
Please Sign up or sign in to vote.

Solution 2

I have made function and enum. For convinience I have created an enum for layer selection, "WhichLayer"
public enum WhichLayer

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
    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;
        case WhichLayer.Blue: ind=0; break;
        case WhichLayer.Green:ind=1; break;
        case WhichLayer.Red: ind = 2;break;
        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...
            pIn += lineOffsetIn;
            pOut += lineOffsetOut;

    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:[^]

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

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