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:
The grid pattern represents transparency; you probably knew that.
Alpha masking, also known as soft masking, is ideal for precisely separating fine picture elements such as hair, fur, or even partially transparent elements such as smoke.
The alpha channel (also called alpha planes) is a color component that represents the degree of transparency (or opacity) of a color (i.e., the red, green and blue channels). It is used to determine how a pixel is rendered when blended with another.
An alpha matte is a video clip, or image, with an alpha channel that is used to define the transparency in another video clip. For example, you might have a scenic video clip of a location.
The alpha channel is a special channel that handles transparency. When an image has an alpha channel on it, it means you can adjust the image's opacity levels and make bits translucent or totally see-through. The alpha channel is instrumental when you want to remove the background from an image.
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.
Depending on your requirements this may be much easier:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With