c# Bitmap.Save 透明度不保存为 png

发布于 2024-08-20 04:43:10 字数 437 浏览 4 评论 0原文

我正在尝试将具有透明度的位图类保存为具有透明度的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

第几種人 2024-08-27 04:43:10

您确定位图的像素格式是 System.Drawing.Imaging.PixelFormat.Format32bppArgb 吗?我只是偶然发现了这个问题,因为我遇到了同样的问题,但这是因为我正在加载一个其像素格式没有 alpha 分量的图像。我这样做了

Bitmap output = original.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

,它正确地保存了带有 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 did

Bitmap output = original.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

and 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.

我是男神闪亮亮 2024-08-27 04:43:10

自从我完成图像编辑/保存以来已经有一段时间了,但如果我没记错的话,PNG 与大多数不同。我认为你必须使用实际的 FileStream。

编辑:啊,找到了一个例子这里

FileStream imageStream= new FileStream( filename, FileMode.Create );
myBitmap.Save( imageStream, ImageFormat.Png );
imageStream.Close();

编辑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

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.

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.

天赋异禀 2024-08-27 04:43:10

您是否尝试过使用 Bitmap.MakeTransparent() 方法?

Have you tried using Bitmap.MakeTransparent() method?

与往事干杯 2024-08-27 04:43:10
ret.MakeTransparent(...);
ret.MakeTransparent(...);
女中豪杰 2024-08-27 04:43:10

原因是 Bitmap 类不支持透明度。

您需要将 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

The reason is that the Bitmap class does not work with transparency.

You need to cast Bitmap to 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
凯凯我们等你回来 2024-08-27 04:43:10

我只是想提醒大家,正如我的许多人所建议的那样,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.

暗喜 2024-08-27 04:43:10

保存为 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.

死开点丶别碍眼 2024-08-27 04:43:10

Portable Network Graphics(.png) 格式支持透明,因此在保存图像时将图像格式设置为 ImageFormat.Png。

        //update image to database
        MemoryStream msImage = new MemoryStream();
        imgPhoto.Save(msImage, System.Drawing.Imaging.ImageFormat.Png);
        byte[] Img = (byte[])msImage.ToArray();

因此,如果以 Jpeg 等任何其他格式保存...将会失去透明度。希望有帮助。

Portable Network Graphhics(.png) formats supports transpareny, so while saving image set image format to 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.

江南月 2024-08-27 04:43:10

我假设对话框的 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.

反话 2024-08-27 04:43:10

虽然问题很老,但这里的代码仍然适合我。

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文