Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make part of an image transparent

Tags:

wpf

xaml

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

like image 856
Tobias Funke Avatar asked Apr 20 '10 17:04

Tobias Funke


2 Answers

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.

like image 179
Charlie Avatar answered Nov 02 '22 11:11

Charlie


Use a paint program (I use Paint.Net) to change the area you want transparent to an alha=0 color. Then save the image (mine was JPG) as a PNG. Seemed to work fine for me in the WPF Image control.

like image 27
BillJam Avatar answered Nov 02 '22 10:11

BillJam