如果检测PNG图像文件是一个透明的形象?是一个、图像文件、透明、形象

2023-09-02 21:35:41 作者:群昵称

我要寻找一种方式来快速确定一个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?

更新:好吧,有没有那么一个不太复杂的方式拉出PNG规范和黑客code?

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

推荐答案

为什么要通过所有的图像,并检查他们的Alpha值?像素的不只是环

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;
    }