Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1050335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:45:20+00:00 2026-05-16T16:45:20+00:00

I’m trying to draw an image, with a source Bitmap and an alpha mask

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:45:20+00:00Added an answer on May 16, 2026 at 4:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.