问题描述
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 不保持透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!