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.
Yes, the faster way to do this is to use
Bitmap.LockBitsand use pointer arithmetic to retrieve the values instead ofGetPixelandSetPixel. 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):
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.