开发者

How can I use a PathGeometry as a mask for a BitmapSource (or any image data)?

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 |                  开发者_如何学JAVA|
 6 --------------------

or

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


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>


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜