Detecting if a PNG image file is a Transparent image?

后端 未结 3 1745
闹比i
闹比i 2020-12-06 05:10

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 a

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 05:35

    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.

提交回复
热议问题