0

In the following code:

    Bitmap bmp = new Bitmap((int)ArrayHeight, (int)ArrayWidth, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    Graphics gBmp = Graphics.FromImage(bmp);
    gBmp.CompositingMode = CompositingMode.SourceCopy;
    System.Drawing.Color green = System.Drawing.Color.FromArgb(0x40, 0, 0xff, 0);
    System.Drawing.Brush greenBrush = new SolidBrush(green);
    gBmp.FillPolygon(greenBrush, polygonPoints);


    for (int i = 0; i < ArrayHeight; i++)
    {
        for (int j = 0; j < ArrayWidth; j++)
        {
            System.Drawing.Color pixel = bmp.GetPixel(i,j);

            if (pixel.IsSystemColor.Equals("green"))
            {
                PolyArray[i, j] = (byte)TerrainValue;
            }
        }
    } 

I want to check to see if a the pixel at that location in the bitmap is the system drawing color 'green', but it is never returning true.

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
zetar
  • 1,225
  • 2
  • 20
  • 45
  • Does `System.Drawing.Color.FromArgb(0x40, 0, 0xff, 0)` == `bmp.GetPixel(i,j).IsSystemColor.Equals("green"))`? – Jay Walker Mar 06 '13 at 20:03
  • I think there is a mismatch in your PixelFormat and thats why the Color values do not match. "Some of the pixel formats contain premultiplied color values. Premultiplied means that the color values have already been multiplied by an alpha value." http://msdn.microsoft.com/en-GB/library/system.drawing.imaging.pixelformat.aspx – Kokulan Eswaranathan Mar 06 '13 at 20:09
  • This link could give you some clue... http://stackoverflow.com/questions/8104461/pixelformat-format32bppargb-seems-to-have-wrong-byte-order – Kokulan Eswaranathan Mar 06 '13 at 20:18

1 Answers1

3

I'm not sure why you need to compare green to a system color. IsSystemColor returns a bool.

But this should work.

if (pixel.IsSystemColor)
{
    PolyArray[i, j] = (byte)TerrainValue;
}

or (I really think this is what you want)

if (pixel == green)
{
    PolyArray[i, j] = (byte)TerrainValue;
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Hey! That does it! Thank you thank you thank you! I had no idea that 'green' was defined somewhere. Thanks! – zetar Mar 06 '13 at 20:08
  • @zetar - you defined `green` as a local variable of type `Color` - `System.Drawing.Color green = System.Drawing.Color.FromArgb(0x40, 0, 0xff, 0);`. You could have called it `donut` and it would have the same meaning. – Jay Walker Mar 06 '13 at 20:53