tags:

views:

31

answers:

1

I want to put an image on a button, but I want part of the image to be transparent. How do I do this?

A: 

Try the Image.OpacityMask property. You can give it a brush that specifies the region you want to be transparent.

EDIT: From MSDN:

There is no direct support for color-keying a bitmap in WPF. However, it is fairly easy to implement on your own. Dwayne has implemented a ColorKeyBitmap on his blog: http://blogs.msdn.com/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx I believe it links to the code on Codeplex as well. You could also accomplish this simply by reading your bitmap into system memory, iterating through all the pixels and setting their values yourself, and constructing a new bitmap out of that array.

Charlie
Can you make the opacity mask target a color instead of a region?
Tobias Funke
I've seen images that have purple backgrounds. There's a way to force that color to become transparent. Is there a way to do this in WPF?
Tobias Funke
Dwayne's project does not build, so I cannot use it. I'm puzzled here. Putting an image on a button is a standard practice. Are you saying there's no easy way to do this in WPF?
Tobias Funke
No it's incredibly easy to put an Image on a Button. As simple as this:<Button> <Image/></Button>What is not easy is setting a transparent color key. You might want to try the approach of reading the bitmap into memory and setting the pixel values yourself.
Charlie