c# Bitmap.Save 透明度不保存为 png
我正在尝试将具有透明度的位图类保存为具有透明度的 png 文件。 我运气不好。
位图具有透明度,只是不保存透明度。
这就是我正在做的
位图设置
Bitmap ret = new Bitmap(bWidth, bHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
保存
ret.Save(filename, ImageFormat.Png);
我也尝试使用文件流保存文件,但这没有什么区别。
当图像位于图片框中时,存在透明度,但是当我保存时,我只会得到黑色背景。
我真的不想使用任何第三方代码,他们找到了一种方法来做到这一点,我也想这样做。
谢谢。
I'm trying to save a Bitmap class that has transparancy as a png file with transparancy.
I'm having no luck.
The bitmap has transparancy, it just doesn't save with transparancy.
this is what I'm doing
bitmap setup
Bitmap ret = new Bitmap(bWidth, bHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
saveing
ret.Save(filename, ImageFormat.Png);
I also tried saving the file with a filestream, and that made no difference.
When the image is in a Picture box the transparancy exists, but when I save i I just get a black background.
I really don't want to use any 3rd party code, they found a way to do it, I'd like to also.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您确定位图的像素格式是 System.Drawing.Imaging.PixelFormat.Format32bppArgb 吗?我只是偶然发现了这个问题,因为我遇到了同样的问题,但这是因为我正在加载一个其像素格式没有 alpha 分量的图像。我这样做了
,它正确地保存了带有 alpha 分量的 PNG。
另外,如果您使用
MakeTransparent()
,请确保您要设为透明的颜色存在于图像中。Are you sure the pixel format of the Bitmap is
System.Drawing.Imaging.PixelFormat.Format32bppArgb
? I just stumbled on this question because I was having the same problem, but it was because I was loading an image which had no alpha component to its pixel format. I didand it properly saved the PNG with the alpha component.
Also, if you're using
MakeTransparent()
be sure that the color you're making transparent exists in your image.自从我完成图像编辑/保存以来已经有一段时间了,但如果我没记错的话,PNG 与大多数不同。我认为你必须使用实际的 FileStream。
编辑:啊,找到了一个例子这里
编辑2:经过更多研究我认为只有在某些情况下才需要中间步骤。
也有可能因为您使用“MakeTransparent”,所以它正在捕获索引的 alpha,但尝试根据每个像素的实际 alpha 值进行保存。您可以尝试实际设置图像的 Alpha 值。
Been a while since I've done image editing/saving but if I remember right PNGs are different than most. I think you have to use an actual FileStream.
EDIT: Ah, found an example here
EDIT2: After more research on this I think the intermediary step is only required under certain circumstances.
It's also possible that because you're using "MakeTransparent" it's catching an indexed alpha, but trying to save based on the actual alpha value of each pixel. You might try actually setting the alpha values of the image.
您是否尝试过使用 Bitmap.MakeTransparent() 方法?
Have you tried using
Bitmap.MakeTransparent()
method?原因是 Bitmap 类不支持透明度。
您需要将
Bitmap
转换为Image
。The reason is that the
Bitmap
class does not work with transparency.You need to cast
Bitmap
toImage
.我只是想提醒大家,正如我的许多人所建议的那样,MakeTransparent 仅使特定颜色透明。它不考虑 argb 图像的 Alpha 通道。因此,例如,alpha 值为 100 的像素,如果它与提供给 MakeTransparent 的颜色不匹配,则不会具有部分透明度。
I just wanted to remind everyone that MakeTransparent, as has been suggested my many individuals here, only makes the specific color transparent. It does not take into account the Alpha Channel of the argb image. So a pixel with an alpha value of 100, for instance, if it does not match the color provided to MakeTransparent, will not have partial transparency.
保存为 PNG 需要可查找流,例如 FileStream 或 MemoryStream。如果您保存到其中之一并从那里获取,将不会出现 GDI+ 异常或类似情况。希望这有帮助。
Saving as PNG REQUIRES seekable stream like FileStream or MemoryStream. If you save into one of there and get from there there will be noe GDI+ exception or similar. Hope this helps.
Portable Network Graphics(.png) 格式支持透明,因此在保存图像时将图像格式设置为 ImageFormat.Png。
因此,如果以 Jpeg 等任何其他格式保存...将会失去透明度。希望有帮助。
Portable Network Graphhics(.png) formats supports transpareny, so while saving image set image format to ImageFormat.Png.
So if saving in any other formats like Jpeg... will make lose transparency. Hope it helps.
我假设对话框的 FilterIndex 从 0 开始...但它实际上从 1 开始,所以我的图像使用 Alpha 透明度保存为 Gif,而 gif 不支持 Alpha 透明度。所以我的问题实际上是对话框。
I assumed that the FilterIndex of a dialog box started at 0...but it actually starts at 1, so my images were being saved as Gifs using alpha transparancy, and gif doesn't support alpha transparency. So my problem was actually with the dialog box.
虽然问题很老,但这里的代码仍然适合我。
Though the Question is very old but still here is the code working for me.