Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert 24Bit to 32Bit with alpha in C#

Posted on 2009-05-08
5
Medium Priority
?
2,445 Views
Last Modified: 2012-05-06
The Code I wrote below should work. But it will not convert a 24 bit to 32 bit image. It should but apparently Clone doesn't work that way.

Anyone have any alternate solutions?
public static Bitmap SetAlpha(Bitmap Target, int alpha)
        {
 
            /*if (Target.PixelFormat == PixelFormat.Format24bppRgb)
            {
 
                Bitmap newBitmap = new Bitmap(Target.Width, Target.Height, PixelFormat.Format32bppArgb);
 
                BitmapData returnData1 = Target.LockBits(new Rectangle(0, 0, Target.Width, Target.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, Target.PixelFormat);
                BitmapData returnData2 = newBitmap.LockBits(new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, newBitmap.PixelFormat);
 
 
 
            }*/
 
            //Clone the Target to return Bitmap, this will change to pixel format correctly
            Bitmap returnBitmap = Target.Clone(new Rectangle(0, 0, Target.Width, Target.Height), PixelFormat.Format32bppArgb);
 
 
 
            //New fast Code using Lock bits to get and set alpha channel. Don't use Get and Set Pixel it's 10x slower
            BitmapData returnData = returnBitmap.LockBits(new Rectangle(0, 0, returnBitmap.Width, returnBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, returnBitmap.PixelFormat);
 
            int PixelSize = 4;
 
            unsafe
            {
 
                for (int y = 0; y < returnData.Height; y++)
                {
                    //Gets the pixelRow for Target Bitmap
                    byte* row = (byte*)returnData.Scan0 + (y * returnData.Stride);
 
                    for (int x = 0; x < returnData.Width; x++)
                    {
                        //Set alpha pixel byte to the same as the mask
 
                        byte oldAlpha = row[x * PixelSize + 3];
                        byte B = row[x * PixelSize + 2];
                        byte G = row[x * PixelSize + 1];
                        byte R = row[x * PixelSize + 0];
                        byte setAlpha = Convert.ToByte(alpha);
 
 
                        //Check for Null pixel
                        if ((R == 0) && (G == 0) && (B == 0) && (oldAlpha < 10)) { R = 254; B = 254; G = 254; setAlpha = oldAlpha; }
                        
 
                        //if (returnBitmap.Height == 290)
                        //{ B = 254; G = 254; R = 254; }
                        //if (alpha > oldAlpha)
                        row[x * PixelSize + 0] = R;
                        row[x * PixelSize + 1] = G;
                        row[x * PixelSize + 2] = B;
                        row[x * PixelSize + 3] = setAlpha;
                    }
 
                }
            }
 
 
            //Restore Bitmap Data Directly back to the bitmap.
            returnBitmap.UnlockBits(returnData);
            
 
            //Dispose of input bitmaps
            Target.Dispose();
 
            return returnBitmap;
        }

Open in new window

0
Comment
Question by:RDdice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
anyoneis earned 1500 total points
ID: 24350255
I don't know why the clone doesn't work as you are using it. Try this tweak:

            //Clone the Target to return Bitmap, this will change to pixel format correctly
            Bitmap returnBitmap = new Bitmap(Target);
            returnBitmap = returnBitmap.Clone(new Rectangle(0, 0, Target.Width, Target.Height), PixelFormat.Format32bppArgb);

dwr
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 24350272
0
 
LVL 7

Author Comment

by:RDdice
ID: 24350286
Ya doesn't work. Passing a 24 bit bitmap to it, it doesn't always return a 32 bit for some reason and i'm not sure why. Could be something to do with the bitmaps I'm using.

Bitmap.MakeTransparent Does return a 32 bit in every instance, but of course makes 1 color transparent.
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 24350422
I can't get it to fail, but I am not trying a bunch of different images. Sorry that didn't help (but it was fun playing with it :-)

Are you saying that a given image does not always convert, or that some images never convert with this code?
0
 
LVL 7

Author Closing Comment

by:RDdice
ID: 31579630
Clone works, but not 100% of the time, seems like an internal bug defendant on the target image.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question