問題描述
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 不保持透明度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!