Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, the faster way to do this is to use <code>Bitmap.LockBits</code> and use pointer arithmetic to retrieve the values instead of <code>GetPixel</code> and <code>SetPixel</code>. 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).</p> <p>For example, you could do something like this (not tested, use at your own risk):</p> <pre><code>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 &lt; 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 &lt; 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(...); </code></pre> <p>This example derives the alpha channel in the output from the <strong>blue</strong> channel in the mask image. I’m sure you can change it to use the mask’s red or alpha channel if required.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload