12

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 doing

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.

Kelly Elton
  • 4,373
  • 10
  • 53
  • 97

10 Answers10

10

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.

haldo
  • 14,512
  • 5
  • 46
  • 52
Brian
  • 5,826
  • 11
  • 60
  • 82
3

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.

McAden
  • 13,714
  • 5
  • 37
  • 63
3

Have you tried using Bitmap.MakeTransparent() method?

haldo
  • 14,512
  • 5
  • 46
  • 52
  • No the bitmap has transparancy, it just doensn't save the transparacny. – Kelly Elton Feb 05 '10 at 08:13
  • Can you provide more code then? Also, do you ever call Bitmap.MakeTransparent? –  Feb 05 '10 at 08:18
  • I do use the Bitmap.MakeTransparent(Color.Transparent) is how I do it. then I use the Graphics class and do a .Clear(Color.Transparent). Then I draw things with the Graphics class, and the resulting bitmap displays the transparency in the program(in a picture box), but when saved as a png, just displays black. – Kelly Elton Feb 05 '10 at 08:25
  • Did you just try saving as Bitmap.Save("file.png")? –  Feb 05 '10 at 08:27
  • just did, doesn't change anything. – Kelly Elton Feb 05 '10 at 08:32
  • Did you consider that you might have a bug in your code? Providing your code here would better help people answer the question. –  Feb 05 '10 at 08:43
  • I should have provided enough...again my problem is the saving of an image that already has transparany...I believe I put enough relevent code in this post. – Kelly Elton Feb 05 '10 at 08:53
  • Did you try Bitmap.MakeTransparent(Color.Black) (instead of Color.Transparent) and see if you really get a saved transparent file? btw, it might be better if you edit your question to say what you are doing, rather than leave it here in the comments. –  Feb 05 '10 at 09:01
  • heh...It was a coding error on my part. I was misreading the file extension...it was actually calling SaveFile(filename, ImageFormat.Bitmap) – Kelly Elton Feb 05 '10 at 18:39
  • 2
    :-). It is usually a good idea to cut&paste your code here, rather than type out what you think it is. –  Feb 05 '10 at 18:48
  • basically I assumed that the FilterIndex of a dialog box started at 0...but for some dumb reason it starts at 1. I would have posted everything but it's too complex and large. Most of the posts would have been people asking questions. – Kelly Elton Feb 05 '10 at 18:51
1

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
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Anton
  • 67
  • 1
  • 1
1
ret.MakeTransparent(...);
mxmissile
  • 11,464
  • 3
  • 53
  • 79
1

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.

Greg
  • 19
  • 4
0

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.

Vlado
  • 1
0

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.

Hari
  • 1
  • 1
0

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();
haldo
  • 14,512
  • 5
  • 46
  • 52
0

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.

Kelly Elton
  • 4,373
  • 10
  • 53
  • 97