As I\'m bringing in images into my program, I want to determine if:
#1
I get a more advanced solution, based on ChrisF answer:
public bool IsImageTransparent(Bitmap image,string optionalBgColorGhost)
{
for (int i = 0; i < image.Width; i++)
{
for (int j = 0; j < image.Height; j++)
{
var pixel = image.GetPixel(i, j);
if (pixel.A != 255)
return true;
}
}
//Check 4 corners to check if all of them are with the same color!
if (!string.IsNullOrEmpty(optionalBgColorGhost))
{
if (image.GetPixel(0, 0).ToArgb() == GetColorFromString(optionalBgColorGhost).ToArgb())
{
if (image.GetPixel(image.Width - 1, 0).ToArgb() == GetColorFromString(optionalBgColorGhost).ToArgb())
{
if (image.GetPixel(0, image.Height - 1).ToArgb() ==
GetColorFromString(optionalBgColorGhost).ToArgb())
{
if (image.GetPixel(image.Width - 1, image.Height - 1).ToArgb() ==
GetColorFromString(optionalBgColorGhost).ToArgb())
{
return true;
}
}
}
}
}
return false;
}
public static Color GetColorFromString(string colorHex)
{
return ColorTranslator.FromHtml(colorHex);
}
It has a optional bg color string to non transparent images:
Example of usage:
IsImageTransparent(new Bitmap(myImg),"#FFFFFF");