I\'m trying to save a Bitmap class that has transparancy as a png file with transparancy. I\'m having no luck.
Have you tried using Bitmap.MakeTransparent()
method?
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 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.
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.