Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display PNG with alpha channel in C#

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

like image 335
rafalry Avatar asked Jun 24 '10 11:06

rafalry


People also ask

Does PNG have alpha layer?

PNG does not support alpha channels, only alpha transparency when you save it as PNG24. If you need to have Alpha channels in Photoshop you have to save as PSD. PNG does not support alpha channels, only alpha transparency when you save it as PNG24.

Is PNG a 4 channel?

PNG images usually have four channels. Three color channels for red, green and blue, and the fourth channel is for transparency, also called alpha channel. In this tutorial, we will learn how to read a PNG image with transparency. The syntax of imread() function contains a second argument whose default value is cv2.

Is PNG alpha transparency?

PNG files support transparency, but JPGs do not. If you save a PNG image as a JPG file, the JPG format doesn't know what to do with the alpha channel. That's why all the transparency turns into an opaque white background instead.

What is PNG with Alpha?

An alpha channel, representing transparency information on a per-pixel basis, can be included in grayscale and truecolor PNG images. An alpha value of zero represents full transparency, and a value of (2^bitdepth)-1 represents a fully opaque pixel.


2 Answers

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.

like image 26
MrFox Avatar answered Oct 29 '22 20:10

MrFox


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.

like image 147
Kristoffer Schroeder Avatar answered Oct 29 '22 19:10

Kristoffer Schroeder