将位图保存为 32bpp 不保持透明度

Saving Bitmap as 32bpp doesn#39;t keep transparency(将位图保存为 32bpp 不保持透明度)
本文介绍了将位图保存为 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 不保持透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)