Saving Bitmap as 32bpp doesn#39;t keep transparency(将位图保存为 32bpp 不保持透明度)
问题描述
using (var bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb))
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
g.DrawImage(image, 0, 0);
bmp.Save("image.bmp", ImageFormat.Bmp);
}
问题应该很清楚:为什么保存到 BMP 会破坏 黑色 的透明度,而保存到 PNG 吗?
The question should be clear: why saving to BMP trashes transparency to black, while saving to PNG keeps it ?
澄清一下:图像采用 Format8bppIndexed 格式,其调色板确实包含透明颜色(例如,它可以正确绘制到表单/图片框上)
Just to clarify: image is in Format8bppIndexed format and its palette does contain transparent colors (e.g. it draws correctly onto form/picturebox)
我的错,Bitmap.Save()实际上以Format32bppRgb格式保存BMP,即使位图格式是Format32bppArgb.
My bad, Bitmap.Save() actually saves BMP in Format32bppRgb format, even though the bitmap format is Format32bppArgb.
推荐答案
这是因为 bmp 文件格式默认不支持透明度,而 png 文件格式支持.
It's because by default the bmp file format doesn't support transparency while the png file format does.
如果你想要透明度,你将不得不使用 png.压缩算法是无损的,因此您不会在图像中出现伪影.该文件也将占用更少的磁盘空间.
If you want transparency you are going to have to use png. The compression algorithms are lossless so you're not going to get artefacts in your image. The file will take up less space on disk too.
这篇关于将位图保存为 32bpp 不保持透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将位图保存为 32bpp 不保持透明度


基础教程推荐
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 创建属性设置器委托 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01