c# Bitmap.Save transparancy doesn't save in png
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?