I use a black and white pictures as a mask to generate nice contours after applying a rectangle. Unfortunately to get rid of the black color I use the MakeTransparent
method, unfortunately it is very slow, in my code I have to perform such a procedure twice, which in the case of 20 images takes about 5 seconds. Is there any other solution to speed up this procedure?
Bitmap contour = new Bitmap(
imageMaskCountour.Width, imageMaskCountour.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(contour))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.FillRectangle(ContourBrush, 0, 0, contour.Width, contour.Height);
g.DrawImage(
imageMaskCountour,
new Rectangle(0, 0, contour.Width, contour.Height),
new Rectangle(0, 0, imageMaskCountour.Width, imageMaskCountour.Height),
GraphicsUnit.Pixel);
}
contour.MakeTransparent(Color.Black);
Edit:
I try add LockBitmap and add the following method:
public void MakeTransparent(Color color)
{
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
if (this.GetPixel(x, y) == color)
{
this.SetPixel(x, y, Color.Transparent);
}
}
}
}
But it is much slower.