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

感情迁移 提交于 2019-12-24 09:39:12

问题


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 ---------

回答1:


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>



回答2:


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?



来源:https://stackoverflow.com/questions/2610114/how-can-i-use-a-pathgeometry-as-a-mask-for-a-bitmapsource-or-any-image-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!