views:

782

answers:

5

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.

+1  A: 

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
I tried that already and it didn't make a difference. thanks anyhow though.
kelton52
Edited and added another possible solution.
McAden
Edit the alpha values of the image?
kelton52
The pixels. Initialize all values to 0 alpha, then do your editing. You can use LockBits.
McAden
I think that might be it...I'll try it tomorrow and see what happens.
kelton52
I tried SetPixel to set all the pixels to argb(0,255,255,255) and that didn't change anything eather.
kelton52
+1  A: 
ret.MakeTransparent(...);
mxmissile
No the bitmap has transparancy, it just doesn't save the transparancy.
kelton52
+1  A: 

Have you tried using Bitmap.MakeTransparent() method?

Moron
No the bitmap has transparancy, it just doensn't save the transparacny.
kelton52
Can you provide more code then? Also, do you ever call Bitmap.MakeTransparent?
Moron
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.
kelton52
Did you just try saving as Bitmap.Save("file.png")?
Moron
just did, doesn't change anything.
kelton52
Did you consider that you might have a bug in your code? Providing your code here would better help people answer the question.
Moron
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.
kelton52
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.
Moron
heh...It was a coding error on my part. I was misreading the file extension...it was actually calling SaveFile(filename, ImageFormat.Bitmap)
kelton52
Moron
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.
kelton52
A: 

basically I assumed that the FilterIndex of a dialog box started at 0...but for some dumb reason it starts at 1, so my images were being saved as Gifs using alpha transparancy, and gif doesn't support alpha transparancy. My bad.

kelton52
A: 

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