Alpha masking in c# System.Drawing?
Asked Answered
T

2

20

I'm trying to draw an image, with a source Bitmap and an alpha mask Bitmap, using the System.Drawing.Graphics object. At the moment I loop X and Y and use GetPixel and SetPixel to write the source color and mask alpha to a third Bitmap, and then render that. However this is very inefficient and I am wondering if there is an faster way to achieve this?

The effect I'm after looks like this:

Effect I’m after

The grid pattern represents transparency; you probably knew that.

Trevino answered 6/9, 2010 at 20:48 Comment(2)
Since you say you are using System.Drawing.Graphics, this is probably off-topic, but here's a link to opacity masks in WPF, which will take advantage of hardware acceleration: msdn.microsoft.com/en-us/library/…Coquelicot
off-topic, but fascinating nonetheless - thanks!Trevino
W
16

Yes, the faster way to do this is to use Bitmap.LockBits and use pointer arithmetic to retrieve the values instead of GetPixel and SetPixel. The downside, of course, is that you have to use unsafe code; if you make a mistake, you can cause some really bad crashes in your program. But if you keep it simple and self-contained, it should be fine (hey, if I can do, you can do it too).

For example, you could do something like this (not tested, use at your own risk):

Bitmap mask = ...;
Bitmap input = ...;

Bitmap output = new Bitmap(input.Width, input.Height, PixelFormat.Format32bppArgb);
var rect = new Rectangle(0, 0, input.Width, input.Height);
var bitsMask = mask.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bitsInput = input.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bitsOutput = output.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unsafe
{
    for (int y = 0; y < input.Height; y++)
    {
        byte* ptrMask = (byte*) bitsMask.Scan0 + y * bitsMask.Stride;
        byte* ptrInput = (byte*) bitsInput.Scan0 + y * bitsInput.Stride;
        byte* ptrOutput = (byte*) bitsOutput.Scan0 + y * bitsOutput.Stride;
        for (int x = 0; x < input.Width; x++)
        {
            ptrOutput[4 * x] = ptrInput[4 * x];           // blue
            ptrOutput[4 * x + 1] = ptrInput[4 * x + 1];   // green
            ptrOutput[4 * x + 2] = ptrInput[4 * x + 2];   // red
            ptrOutput[4 * x + 3] = ptrMask[4 * x];        // alpha
        }
    }
}
mask.UnlockBits(bitsMask);
input.UnlockBits(bitsInput);
output.UnlockBits(bitsOutput);

output.Save(...);

This example derives the alpha channel in the output from the blue channel in the mask image. I’m sure you can change it to use the mask’s red or alpha channel if required.

Windup answered 6/9, 2010 at 20:50 Comment(5)
Yes, but it’s very fast. Any other implementation that you could potentially use would equally have to iterate through the pixels.Windup
Well, it works, and it's definitely faster than my previous method, thank you!Trevino
I needed this for something unrelated, it works like a champ, thank you!Prune
Note that you do not need to use unsafe code - you can use Marshal.Copy to copy to/from the BitmapData.Scan0 managed pointer (IntPtr), then manipulate a managed byte array. The overheads are small.Piero
FastBitmap does something similarUphroe
A
6

Depending on your requirements this may be much easier:

  • Invert your mask so that the circle is transparent and the rest is from a color not used in your input bitmap (like red in your example)
  • Draw your mask on top of your image using Graphics.FromImage(image).DrawImage(mask)...
  • Set the mask color as transparent on your image (image.MakeTransparent(Color.Red))

The only drawback of this method is that it needs you to make sure the mask color is not used in your image. I don't know if this is slower or faster than the manual way.

Alage answered 29/9, 2011 at 14:47 Comment(1)
In my scenario (big bitmap) this would be the fastestUphroe

© 2022 - 2024 — McMap. All rights reserved.