Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the transparency of a WPF ImageSource (equivalent to GDI Matrix33 = 0.5f)?

Tags:

c#

wpf

Does WPF have an equivalent to this?

ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(new ColorMatrix { Matrix33 = 0.5f }, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

In other words, can I adjust the transparency of a WPF ImageSource (or any other drawing related class e.g. BitmapImage), or is GDI the best choice here?

I don't intend to draw the image onto a window, so I think that rules out using the Image class(?)

(My line of thought with trying to use WPF instead of GDI is primarily because I'm under the impression that with WPF I can have hardware acceleration, but from what I've seen so far, it seems that it's only applying to a very limited subset of image manipulation)

like image 251
unrelativity Avatar asked Sep 20 '25 07:09

unrelativity


1 Answers

check out WriteableBitmapEx, which provides a set of extension methods for manipulating bitmaps in memory with WPF and Silverlight.

There is nothing in WPF per-se that will allow you to do want, but the WPF/Silverlight class WriteableBitmap provides a fast, low-level API to drawing/manipulating and is native to WPF.

WriteableBitmapEx then builds on this by providing extension methods to do GDI-like operations. There is a Convolute function which allows convolving an image with a matrix, so you could do the above. Note WB-Ex is a silverlight library but they also provide an unmaintained WPF version which although incomplete, can be extended to keep up to date with the Silverlight version.

Other than that I'd suggest rolling your own. If all you wish to do is modify the opacity to 0.5f then rather than convolve I'd suggest writing a specific function to do that based on the above examples

like image 103
Dr. Andrew Burnett-Thompson Avatar answered Sep 21 '25 22:09

Dr. Andrew Burnett-Thompson