我试图将图像转换为png透明图像,但它没有完全转换,因为我可以看到白色但不是精确的白色(255),值就像(#FBFBFF,#F7FFFF等...)我该如何处理?
图像即将到来 http://i.stack.imgur.com/CbSwe.png
我正在尝试如下
public static Bitmap MakeTransparent(Image image)
{
Bitmap b = new Bitmap(image);
Color redColor = Color.FromArgb(255, 255, 255);
Color redColor1 = Color.FromArgb(254, 254, 254);
Color redColor2 = Color.FromArgb(253, 253, 253);
Color redColor3 = Color.FromArgb(252, 252, 252);
Color redColor4 = Color.FromArgb(251, 251, 251);
Color redColor5 = Color.FromArgb(250, 250, 250);
Color redColor6 = Color.FromArgb(249, 249, 249);
Color redColor7 = Color.FromArgb(249, 249, 248);
b.MakeTransparent(redColor);
b.MakeTransparent(redColor1);
b.MakeTransparent(redColor2);
b.MakeTransparent(redColor3);
b.MakeTransparent(redColor4);
b.MakeTransparent(redColor5);
b.MakeTransparent(redColor6);
b.MakeTransparent(redColor7);
return b;
}
但没有用。
答案 0 :(得分:1)
如果用白色替换所有近白色,则可以使白色本身透明:
public static Bitmap MakeTransparent(Image image)
{
Bitmap b = new Bitmap(image);
var replacementColour = Color.FromArgb(255, 255, 255);
var tolerance = 10;
for (int i = b.Size.Width - 1; i >= 0; i--)
{
for (int j = b.Size.Height - 1; j >= 0; j--)
{
var col = b.GetPixel(i, j);
if (255 - col.R < tolerance &&
255 - col.G < tolerance &&
255 - col.B < tolerance)
{
b.SetPixel(i, j, replacementColour);
}
}
}
b.MakeTransparent(replacementColour);
return b;
}
答案 1 :(得分:0)
foreach (var color in GetColors(10))
{
b.MakeTransparent(color);
}
IEnumerable<Color> GetColors(int interval)
{
for (var r = 255; r > 255 - interval; --r)
{
for (var g = 255; g > 255 - interval; --g)
{
for (var b = 255; b > 255 - interval; --b)
{
yield return Color.FromArgb(r, g, b);
}
}
}
// Or
//return from r in Enumerable.Range(255 - interval, interval)
// from g in Enumerable.Range(255 - interval, interval)
// from b in Enumerable.Range(255 - interval, interval)
// select Color.FromArgb(r, g, b);
}