c# Bitmap.Save transparancy doesn't save in png

前端 未结 10 1373
眼角桃花
眼角桃花 2020-12-03 15:38

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 wi

相关标签:
10条回答
  • 2020-12-03 16:02

    Have you tried using Bitmap.MakeTransparent() method?

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-03 16:05

    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.

    0 讨论(0)
  • 2020-12-03 16:08

    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.

    0 讨论(0)
提交回复
热议问题