检查图像是否透明

时间:2013-05-10 20:21:45

标签: c# asp.net .net asp.net-mvc-4

我目前正在使用一个检查图像是否为特定大小的函数,如果是,我将其转换为jpeg(例如:如果它是一个大png,则转换为jpeg)。

我想知道是否有一个允许你检查透明度的C#函数。如果图像是透明的,则保持透明并且不进行转换。

任何正确方向的帮助都会很棒。谢谢!

1 个答案:

答案 0 :(得分:1)

我还没有测试过,但也许这个代码段可以正常运行:

System.Drawing.Image myImage; //Set source from image here
System.Drawing.Bitmap myBitmap = new System.Drawing.Bitmap(myImage);

for (xPixel = 0; xPixel <= (myBitmap.Width - 1); xPixel++) {
    for (yPixel = 0; yPixel <= (myBitmap.Height - 1); yPixel++) {
        if (myBitmap.GetPixel(xPixel, yPixel) == Drawing.Color.Transparent) {
            // Image contains transparency
        }
    }
}