C# GDI - How to check if a Pixel is opaque or not?

Posted by rkawano on Stack Overflow See other posts from Stack Overflow or by rkawano
Published on 2013-11-03T03:38:18Z Indexed on 2013/11/03 3:53 UTC
Read the original article Hit count: 163

Filed under:
|
|
|

I am using a method to get a pixel of the image to check if this point is transparent or not. I am using GetPixel that returns a System.Drawing.Color with a 32bit color info.

This struct have the "A" property where I can get the alpha value of pixel, according to this MSDN topic.

Code:

using (Bitmap bmp = new Bitmap(path))
{
    Color pixel = bmp.GetPixel(0, 0);
    if (pixel.A == 0)
        // This is a fully transparent pixel
    else
        // This is not a fully transparent pixel
}

When I use this method with a fully transparent PNG images it returns 0. When I run with a white semi-transparent images, it will give me other values starting on 1 and up to 86, where 86 are given for images with a 100% alfa (full opaque).

But with opaque JPEG images, the "A" property are giving me all sort of values like 56, 71, 86, 129, and others, depending on image. But these pixels are fully opaques!

How are the correct way to check if a pixel is opaque or not?

© Stack Overflow or respective owner

Related posts about c#

Related posts about gdi