tags:

views:

82

answers:

2

Assuming I have a BitmapSource (actually I have access to the raw pixels as well if necessary), how can I use a PathGeometry as a mask to cut out certain parts of the image?

   01234567890123456789
 0 --------------------
 1 |   +     +        |
 2 |      *           |
 3 |          *    )  |
 4 |    *             |
 5 |            (     |
 6 --------------------

Assuming I have a PathGeometry that describes a rectangle that goes from (0, 0) to (8, 3), I would like to be able to get one of the following two images:

   01234567890123456789
 0 --------------------
 1 |   +              |
 2 |      *           |
 3 |                  |
 4 |                  |
 5 |                  |
 6 --------------------

or

   012345678
 0 ---------
 1 |   +   |
 2 |      *|
 3 ---------
A: 

Not what sure what you mean by "cut out" certain parts. That could mean you need to just display regions of the image or you need to actually create a new image of just the cut out part or something different?

My first thought is the simplest solution just set the BitmapImage as the source of an Image element and then set the Image.Clip property to the path geometry that includes the region you want.

That will clip the Image such that only the area you want is shown and anything outside the path geometry of the clip will be "clipped"

You could do this to clip any arbitrary piece of the image.

Is that what you are looking for?

Foovanadil
Either of the resulting images I gave as examples would work :) I think what you're suggesting will probably work, just have to try it out.My preference would be to have a new image of just the "cut out" part.
Jedidja
+2  A: 

Ok so my example should work then. It might not be the most performant depending on your situation but it would be a starting point.

It would look something like this. Obviously the points in the clip would be different for your situation but you get the idea.

<Image Source="SomeImage.jpg">
            <Image.Clip>
                <PathGeometry>
                    <PathFigure StartPoint="0,0"
                                IsClosed="True">
                        <LineSegment Point="25,0" />
                        <LineSegment Point="25,25" />
                        <LineSegment Point="0,25" />
                    </PathFigure>
                </PathGeometry>
            </Image.Clip>
        </Image>
Foovanadil
Will try it out; need to do it in code rather than XAML but the idea seems solid.
Jedidja