c# Bitmap.Save transparancy 不保存为 png
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2205742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# Bitmap.Save transparancy doesn't save in png
提问by Kelly Elton
I'm trying to save a Bitmap class that has transparancy as a png file with transparancy. I'm having no luck.
我正在尝试将具有透明度的 Bitmap 类保存为具有透明度的 png 文件。我没有运气。
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.
我真的不想使用任何 3rd 方代码,他们找到了一种方法,我也想这样做。
thanks.
谢谢。
采纳答案by Kelly Elton
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.
我假设对话框的 FilterIndex 从 0 开始……但它实际上从 1 开始,所以我的图像使用 alpha 透明度保存为 Gif,而 gif 不支持 alpha 透明度。所以我的问题实际上出在对话框上。
回答by McAden
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.
自从我完成图像编辑/保存以来已经有一段时间了,但如果我没记错的话,PNG 与大多数不同。我认为您必须使用实际的 FileStream。
EDIT: Ah, found an example here
编辑:啊,在这里找到了一个例子
FileStream imageStream= new FileStream( filename, FileMode.Create );
myBitmap.Save( imageStream, ImageFormat.Png );
imageStream.Close();
EDIT2: After more research on this I think the intermediary step is only required under certain circumstances.
EDIT2:在对此进行更多研究后,我认为仅在某些情况下才需要中间步骤。
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.
也有可能因为您使用的是“MakeTransparent”,它捕获了索引 alpha,但尝试根据每个像素的实际 alpha 值进行保存。您可以尝试实际设置图像的 alpha 值。
回答by mxmissile
ret.MakeTransparent(...);
回答by mxmissile
Have you tried using Bitmap.MakeTransparent()
method?
你试过使用Bitmap.MakeTransparent()
方法吗?
回答by Vlado
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.
保存为 PNG 需要可查找的流,如 FileStream 或 MemoryStream。如果您保存到其中之一并从那里获得,则不会出现 GDI+ 异常或类似异常。希望这可以帮助。
回答by Brian
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 did
你确定位图的像素格式是 System.Drawing.Imaging.PixelFormat.Format32bppArgb
?我只是偶然发现了这个问题,因为我遇到了同样的问题,但这是因为我正在加载一个像素格式没有 alpha 分量的图像。我做了
Bitmap output = original.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
and it properly saved the PNG with the alpha component.
它正确地保存了带有 alpha 组件的 PNG。
Also, if you're using MakeTransparent()
be sure that the color you're making transparent exists in your image.
此外,如果您正在使用,请MakeTransparent()
确保您的图像中存在您要透明的颜色。
回答by Anton
The reason is that the Bitmap
class does not work with transparency.
原因是Bitmap
该类不具有透明度。
You need to cast Bitmap
to Image
.
您需要投射Bitmap
到Image
.
Bitmap ret = new Bitmap(bWidth, bHeight,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
ret.MakeTransparent(Color.White); // Change a color to be transparent
Image img = (Image) ret;
img.Save(filename, ImageFormat.Png); // Correct PNG save
回答by Hari
Portable Network Graphhics(.png) formats supports transpareny, so while saving image set image format to ImageFormat.Png.
Portable Network Graphhics(.png) 格式支持透明,因此同时将图像集图像格式保存为 ImageFormat.Png。
//update image to database
MemoryStream msImage = new MemoryStream();
imgPhoto.Save(msImage, System.Drawing.Imaging.ImageFormat.Png);
byte[] Img = (byte[])msImage.ToArray();
So if saving in any other formats like Jpeg... will make lose transparency. Hope it helps.
因此,如果以 Jpeg 等任何其他格式保存...将失去透明度。希望能帮助到你。
回答by Greg
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.
我只是想提醒大家,MakeTransparent,正如我这里的许多人所建议的那样,只会使特定颜色透明。它没有考虑 argb 图像的 Alpha 通道。因此,例如,alpha 值为 100 的像素,如果它与提供给 MakeTransparent 的颜色不匹配,则不会具有部分透明度。
回答by Gaurav Lal
Though the Question is very old but still here is the code working for me.
虽然问题很老,但仍然是对我有用的代码。
String jpg1 = FrameImageFilePath;
String jpg2 = InnerImageFilePath;
String jpg3 = OutputFilePath;
Image img1 = Image.FromFile(jpg1);
Image img2 = Image.FromFile(jpg2);
int width = img1.Width;
int height = img1.Height;
Bitmap img3 = new Bitmap(img1.Width, img1.Height);
Bitmap img2Resized = new Bitmap(img2, width, height);
Graphics g = Graphics.FromImage(img3);
g.Clear(Color.Black);
g.DrawImage(img2Resized, new Point(0, 0));
g.DrawImage(img1, new Point(0, 0));
g.Dispose();
img1.Dispose();
img2.Dispose();
img3.Save(jpg3, System.Drawing.Imaging.ImageFormat.Jpeg);
img3.Dispose();