Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if a PNG image file is a Transparent image?

Tags:

c#

.net

image

png

I am looking for a way to quickly determine if a PNG image has transparent features. That is, whether any portion of the image is translucent or displays the background in any way. Does anyone one know a simple way to detect this?

UPDATE: OK, is there a less complicated way then to pull out the PNG specification and hacking code?

like image 632
kenny Avatar asked Apr 02 '10 22:04

kenny


People also ask

How can I tell if a PNG is transparent?

Images that have transparency often illustrate it by using a gray and white checkered pattern. The idea is that you can see which parts of the image will be transparent before you save it. The checkered pattern is the background.

How do you make sure a PNG has a transparent background?

Click the Lasso Tool on the Tools panel. Hold and drag your cursor around the object you want to remove from the background. Create a new layer by clicking the New Layer icon at the bottom of the Layers panel. Make sure that the new layer is transparent.

How do I search for a transparent PNG?

Type in your search term and run your search as normal. After you get your results, click on "Tools" in the top menu to see the advanced search options. In the "Color" drop down menu choose the option for "Transparent". The results you get now will be images that have a transparent portion.


1 Answers

Why not just loop through all of the pixels in the image and check their alpha values?

    bool ContainsTransparent(Bitmap image)
    {
        for (int y = 0; y < image.Height; ++y)
        {
            for (int x = 0; x < image.Width; ++x)
            {
                if (image.GetPixel(x, y).A != 255)
                {
                    return true;
                }
            }
        }
        return false;
    }
like image 97
Adam P Avatar answered Sep 20 '22 06:09

Adam P