使用 LoadBitmap 不会在图像中保留 Alpha 通道
我正在尝试从非托管文件加载一些图像,但遇到了 alpha 未通过的问题。我发现我可以通过这样做来恢复 alpha 通道:
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);
当桌面设置为 32 位颜色时,这效果很好,但由于某种原因,当桌面设置为 16 位颜色时,bmpSource 只是一个 16 位颜色图像,即使资源文件中的源图像实际上是带有 alpha 通道的 32 位图像。如何将这些图像加载为 32 位图像且 Alpha 通道完好无损?是否有一种非托管方法来处理此问题,而不是依赖 C# Bitmap 类?
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 unmanaged way to handle this instead of relying on the C# Bitmap class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我知道 Image 类(Bitmap 类是其子类)不支持 Bitmap alpha 透明度(他们建议使用 PNG 或 GIF 代替)。坏消息是你必须创建自己的 bmp 加载器或在那里找到一个(我在 C++ 时代使用的那个现在似乎已经死了并且消失了,否则我会链接到它)。好消息是,创建自己的文件格式非常容易:文件格式
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