开发者

c# Bitmap.Save transparancy doesn't save in 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 d开发者_运维问答oing

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.


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.


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.


Have you tried using Bitmap.MakeTransparent() method?


ret.MakeTransparent(...);


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


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.


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


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.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜