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 不保持透明度
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
