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

前端 未结 10 1372
眼角桃花
眼角桃花 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 15:43
    ret.MakeTransparent(...);
    
    0 讨论(0)
  • 2020-12-03 15:44

    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.

    0 讨论(0)
  • 2020-12-03 15:47

    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.

    0 讨论(0)
  • 2020-12-03 15:53

    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.

    0 讨论(0)
  • 2020-12-03 15:54

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

    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.

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