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

  • SEARCH
  • Home
  • 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 6816207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:53:52+00:00 2026-05-26T20:53:52+00:00

I have a bitmap, and I am trying to apply an alpha channel to

  • 0

I have a bitmap, and I am trying to apply an alpha channel to it. This alpha exists in a binary file, and I am able to read all of the bytes in it and get the correct values. Color.FromArgb also gives the right color when I test it. However, when I call SetPixel on my bitmap, it doesn’t work – it acts like nothing happened. What am I doing wrong?

If it is relevant, the bitmap is created from a stream using SharpZibLib.

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
         var c = imageData.GetPixel(x, y);
         var a = alphaMask[y*width + x];

         imageData.SetPixel(x, y, Color.FromArgb(a, c));
    }
}

(OLD) Final Answer: (Has side effects, see new final solution)
It turns out that I needed the speed from CodeInChaos’s answer, but his answer did not work until I also used Vitaliy’s suggestion and did MakeTransparent() before I locked the bits.

(NEW) Final Answer
Using MakeTransparent() on a 24-bit bitmap actually has some undesirable consequences. MakeTransparent() looks for a specific light-grey color to make the transparent color in the bitmap. If this color does not exist, then it chooses the closest color, and sets it to ARGB (00,00,00,00). If you then make this non-transparent by changing the alpha to 255, you get black pixels on your image wherever the ‘closest’ color to the light grey is. I suggest using the following method to “upgrade” your image if you need to add an alpha channel:

            var bmp = new Bitmap(imageStream);
            var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

            var upgradedBmp = bmp.Clone(rect, PixelFormat.Format32bppArgb);
            bmp.Dispose();

This adds the alpha channel without replacing a color to be transparent, and minimal memory usage in the long run.

  • 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-26T20:53:53+00:00Added an answer on May 26, 2026 at 8:53 pm

    Perhaps a pre-multiplied alpha vs non pre-multiplied alpha issue.

    But I’d recommend avoiding GetPixel/SetPixel completely. They are very very slow. Lock the image data and work directly on it.

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

Sidebar

Related Questions

I'm trying to read the bitmap header. I have defined the following struct: typedef
I am trying to apply DCT(discrete cosine transformation) compression on a bmp(bitmap) file. I
i'm trying to dim the alpha channel of individual pixels in a bitmap. i'd
I am trying to store a bitmap (that i have previously read from a
I have converted .jpg images into a bitmap thumbnail. What I am trying to
I have a bitmap image context and want to let this appear blurry. So
I have a simple question. I have this bitmap Is there a way with
I have been trying to figure out a way to convert bitmap files into
I have a Canvas I draw on, I'm trying to take the bitmap out,
I have a source bitmap that is 1x1 and I am trying to take

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.