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?

What exactly are you looking for? The presence of an alpha channel? The declaration of a transparent color in an 8-bit PNG palette? The presence of fully transparent pixels? What about semitransparent pixels and palette entries?
–
Michael MadsenApr 2 '10 at 22:34

I am looking to determine if any aspect of the PNG image that makes any portion of the image translucent and display the background in any way.
–
kennyApr 3 '10 at 1:09

Keep in mind that using GetPixel is atrociously slow. You should use the Bitmap.LockBits method to get a pointer to the image data, and use unsafe code to process the image. The GetPixel method will lock the image bits, get the pixel data, build a Color struct, then unlock the image bits every time it is called, requiring orders of magnitude more processing time.
–
jristaApr 3 '10 at 2:34

I was hoping for something a little quicker than checking every pixel. But it is certainly easier than RTM. I'll have to decide if I have the time for that brute force check. Thanks.
–
kennyApr 3 '10 at 10:45

It would be more efficient to return true from within the loop, therefore exiting immediately if the first pixel is transparent.
–
Tim BookerJul 2 '10 at 10:09

Though it won't help you to determine whether a given png file actually uses transparent features, it will indicate, that it has color type 4 or 6 (both support transparency) according to png file specification.

Thanks, but this isn't helpful for me, since I need to know if the image contained is actually translucent not if it could be. The other answer above works, but I was hoping for something quicker, which this clearly is....
–
kennyApr 3 '10 at 10:49

1

This is quite useful. I have used it in combination with the code above so I only inspect the pixels of images that support transparency.
–
Tim BookerJul 2 '10 at 12:25

Here is an effective approach:
Open the PNG in binary.
Seek to byte 26 (25 if counting from zero).
Evaluate the byte value of the char:
2 or lower => definitely opaque,
3 or higher => supports transparency.
According to my findings, files generated by Photoshop only use 3 or higher when needed making this a reliable way to tell when using these. It appears that almost all of the files have 2 for opaque and 6 for alpha-blended.
You may also consider checking the PNG and IHDR strings found in that general area to fool-proof your code.