gdi+ newbie: image transparency

D

Dan

Let's say I have 2 images A and B: if I want to display B over A with a
varying level of transparency, I can use a matrix to change the alpha value
e.g.

// ... draw image A ...
ColorMatrix cm = new ColorMatrix();
cm.Matrix33 = 50; // 50% opacity
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
// ...draw image B using attributes ia...

Besides this, I'd also like to treat a single color value in B as a the
transparent color: i.e. if the B image has a black circle onto a white
background, I'd like to treat white (rgb FFFFFF) as fully transparent,
independently from the transparency value chosen for blending B with A (the
circle will be drawn onto A with the specified transparency level and its
white background will not be visible at all). How can I do this?

Thanks guys!
 
B

Bob Powell [MVP]

You can use Bitmap.MakeTransparent to set a specific color to transparent
and then draw the image using the ColorMatrix setting.

Note that MakeTransparent actually sets the alpha of a specific colour to
zero and applying that to a second color then makes both transparent. It's
cumulative.

The GDI+ FAQ might also have info that you'll enjoy.

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top