Given an image, I want to convert its pixels nearby a specified color, like Color.FromArgb(210, 189, 51)
, to transparent area.
I tried this:
public Bitmap MakeTransparent(Image image,Color clr)
{
Bitmap b = new Bitmap(image);
var replacementColour = Color.FromArgb(255, 255, 255);
var tolerance = 1;
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 (clr.R - col.R < tolerance &&
clr.G - col.G < tolerance &&
clr.B - col.B < tolerance)
{
b.SetPixel(i, j, replacementColour);
}
}
}
b.MakeTransparent(replacementColour);
return b;
}
But the result is wrong.