wpf - DrawingContext.PushOpacityMask() with BitmapImage does not work -


i want take given bitmapimage , use grayscale/black-and-white representation opacity mask on drawingcontext.

i got far enough color conversion, ended follwing state of demo application:

method on viewmodel:

public void demo()     {         var width = 400;         var height = 400;          var target = new rendertargetbitmap(width, height, 96, 96, pixelformats.pbgra32);         var target2 = new rendertargetbitmap(width, height, 96, 96, pixelformats.pbgra32);          var drawingvisual = new drawingvisual();         var drawingvisual2 = new drawingvisual();          using (var drawingcontext = drawingvisual.renderopen())         {             var bitmapimage = new bitmapimage(new uri(@"pack://application:,,,/warningfilled.png"));              using (var drawingcontext2 = drawingvisual2.renderopen())             {                 drawingcontext2.drawimage(bitmapimage, new rect(0, 0, width, height));             }              target2.render(drawingvisual2);              var mask = target2.tograyscale();             this.mask = mask;              drawingcontext.pushopacitymask(new imagebrush(mask));             drawingcontext.drawrectangle(brushes.red, null, new rect(0, 0, width, height));             // drawingcontext.pop();         }          target.render(drawingvisual);          this.rendered = target;     } 

view:

<window x:class="wpfapplication1.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:wpfapplication1="clr-namespace:wpfapplication1"     title="mainwindow" height="350" width="525"> <window.datacontext>     <wpfapplication1:mainviewmodel></wpfapplication1:mainviewmodel> </window.datacontext> <grid>     <grid.columndefinitions>         <columndefinition width="auto" />         <columndefinition width="auto" />     </grid.columndefinitions>     <border grid.column="0" borderthickness="2" borderbrush="blue" horizontalalignment="center">         <image source="{binding rendered}" width="400" height="400"></image>     </border>     <border grid.column="1" borderthickness="2" borderbrush="green" horizontalalignment="center">         <image source="{binding mask}" width="400" height="400"></image>     </border> </grid> 

running demo application produces this: enter image description here

i expected red rectangle on left reflect opacity mask pushed onto drawingcontext before drawing actual rectangle.

the image see on right bitmapimage after color conversion, right before pushed opacity mask. mentioned before tried black/white conversion, 2 colors there's no effect.

i've tried many similar setups , scenarios, don't opacity mask work.

from opacity masks overview article on msdn (emphasis mine):

to create opacity mask, apply brush opacitymask property of element or visual. brush mapped element or visual, , opacity value of each brush pixel used determine resulting opacity of each corresponding pixel of element or visual.

the color values of pixel ignored.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -