I am trying to load some images from an unmanaged file and running into problems with the alpha not coming through. I have found that I can restore the alpha channel by doing this:
BitmapData bmData =
bmpSource.LockBits(
new Rectangle(0, 0, bmpSource.Width, bmpSource.Height),
ImageLockMode.ReadOnly, bmpSource.PixelFormat);
Bitmap dstBitmap =
new Bitmap(bmData.Width, bmData.Height, bmData.Stride,
PixelFormat.Format32bppArgb, bmData.Scan0);
bmpSource.UnlockBits(bmData);
This works great when the desktop is set to 32bit colour, but for some reason when the desktop is set to 16bit colour, bmpSource is only a 16bit colour image, even though the source image in the resource file is actually a 32bit image with an alpha channel. How can I load these images as 32bit images with the alpha channel intact? Is there an unmana开发者_如何学运维ged way to handle this instead of relying on the C# Bitmap class?
OK, I know that the Image class (which the Bitmap class is a sub class of), doesn't support Bitmap alpha transparency (they suggest using PNGs or GIFs instead). The bad news is you're going to have to create your own bmp loader or find one out there (the one I used back in my C++ days seems to be dead and gone now or I'd link to it). The good news is that it's pretty easy to create your own: File Format
精彩评论