Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...