I'm having a problem with masking an image in WinRT. Basically, what I need to do, is to cut out a puzzle shape out of the base image. I have the puzzle shape as a PNG black and white image, where the shape itself is white and the background black and also as a transparent shape of the puzzle piece. This is actually a port of a iOS app, where they used a CGContextClipToMask with the black and white mask to cut out the puzzle piece. I tried using the Blit from the WriteableBitmapEx to mask the images, but I never achieved the result I wanted, the closest I got was the correctly cut out shape, but with a black background, instead of nothing. What is the correct way of cutting out this shape? Thanks for all the answers!
1
votes
2 Answers
2
votes
Indeed, WinRT/XAML in Windows 8 does not have an OpacityMask
implementation of other XAML frameworks. You could use WriteableBitmap
to manipulate the pixels, but it's a bit slow, especially on ARM devices. A faster solution is to use Direct2D, which has a FillOpacityMask method built right in. Since SharpDX wraps it nicely for .NET you can do that with C# too.
1
votes
I don't have code, however the simplest case would be to just open PNG file in Photoshop/GIMP/any online transparency tool and just map black pixels alpha to zero.
Another example would be doing that in code directly, WriteableBitmapEx has function to change each pixel, all you have to do, is loop through all black pixels and change alpha to 0.