将位图保存为 32bpp 不保持透明度
- 作者: 命中助腚
- 来源: 51数据库
- 2022-10-20
问题描述
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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
