2
votes

I have a bitmap, and I am trying to apply an alpha channel to it. This alpha exists in a binary file, and I am able to read all of the bytes in it and get the correct values. Color.FromArgb also gives the right color when I test it. However, when I call SetPixel on my bitmap, it doesn't work - it acts like nothing happened. What am I doing wrong?

If it is relevant, the bitmap is created from a stream using SharpZibLib.

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
         var c = imageData.GetPixel(x, y);
         var a = alphaMask[y*width + x];

         imageData.SetPixel(x, y, Color.FromArgb(a, c));
    }
}

(OLD) Final Answer: (Has side effects, see new final solution) It turns out that I needed the speed from CodeInChaos's answer, but his answer did not work until I also used Vitaliy's suggestion and did MakeTransparent() before I locked the bits.

(NEW) Final Answer Using MakeTransparent() on a 24-bit bitmap actually has some undesirable consequences. MakeTransparent() looks for a specific light-grey color to make the transparent color in the bitmap. If this color does not exist, then it chooses the closest color, and sets it to ARGB (00,00,00,00). If you then make this non-transparent by changing the alpha to 255, you get black pixels on your image wherever the 'closest' color to the light grey is. I suggest using the following method to "upgrade" your image if you need to add an alpha channel:

            var bmp = new Bitmap(imageStream);
            var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

            var upgradedBmp = bmp.Clone(rect, PixelFormat.Format32bppArgb);
            bmp.Dispose();

This adds the alpha channel without replacing a color to be transparent, and minimal memory usage in the long run.

2
What format are you saving the image as?vcsjones
The image is not being saved, its all being done in memory so it can be previewed.Corey Larson

2 Answers

3
votes

Perhaps a pre-multiplied alpha vs non pre-multiplied alpha issue.

But I'd recommend avoiding GetPixel/SetPixel completely. They are very very slow. Lock the image data and work directly on it.

2
votes

Before calling SetPixel() you need to call MakeTransparnet().

See VB .NET picture GetPixel & SetPixel: Include alpha?