9
votes

Is there a way to properly display an image with alpha channel (let's say PNG) in C# application? Thank you for any suggestions.

UPDATE:

OK, my question was a bit unprecise. I'd like to acquire real transparency of alpha channel - not filling with the parent's background color. In the image below we can see that the transparency is supported, but the part of the button that lies below the image is not visible. Is it possible to have a real transparency of the alpha channel of an image?

image presenting not-total transparency

3
What type of application? winform? wpf? html / ASP.NET? html / ASP.NET MVC? silverlight? xna? phone 7? windows mobile? iPhone? wrist-watch (micro-framework)? gtk# (mono)?Marc Gravell
What does "improperly display" look like?Hans Passant
@Marc Gravell - it's a standard C# windows forms application @Hans Passant - good hint - see UPDATErafalry

3 Answers

8
votes

If that is winforms then no. "transparency" in winforms is not real transparency.

What you can do is to create a alphatransparent form that draws the PNG and position it at the correct location and bind move etc. alt texthttp://www.codeproject.com/KB/GDI-plus/perpxalpha_sharp.aspx

PerPixelAlphaForm transparentImageForm = new PerPixelAlphaForm();
transparentImageForm.SetBitmap(<IMAGE GOES HERE>,<OPACITY GOES HERE>);

//opacity is the opacity that the image will be drawn with, a value of 255 = all transparent parts will be alpha/transparent just as much as the original PNG etc..

EDIT: OR GO TO WPF.

3
votes

Yes, System.Drawing.Image.FromFile("filename.png"); The .NET framework supports transparency for multiple filetypes, I din't think it worked with JPEG, but PNG should be fine.

0
votes

The following MSDN link will help:

WPF: http://msdn.microsoft.com/en-us/library/aa970062.aspx

Windows: http://msdn.microsoft.com/en-us/library/stf701f5.aspx

About System.Drawing.Image.FromFile method:

Managed GDI+ has built-in encoders and decoders that support the following file types:
- BMP
- GIF
- JPEG
- PNG
- TIFF