如何使用 PathGeometry 作为 BitmapSource(或任何图像数据)的掩码?

发布于 2024-08-28 11:33:08 字数 688 浏览 9 评论 0原文

假设我有一个 BitmapSource(实际上如果需要的话我也可以访问原始像素),我如何使用 PathGeometry 作为遮罩来剪切图像的某些部分?

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

假设我有一个 PathGeometry 描述从 (0, 0) 到 (8, 3) 的矩形,我希望能够获得以下两个图像之一:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2024-09-04 11:33:08

好的,那么我的例子应该可以工作了。根据您的情况,它可能不是性能最好的,但这将是一个起点。

它看起来像这样。显然,剪辑中的要点会因您的情况而有所不同,但您明白了。

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

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>
千仐 2024-09-04 11:33:08

不知道你所说的“剪掉”某些部分是什么意思。这可能意味着您只需要显示图像的区域,或者您需要实际创建仅剪切部分或不同内容的新图像?

我的第一个想法是最简单的解决方案,只需将 BitmapImage 设置为 Image 元素的源,然后将 Image.Clip 属性设置为包含所需区域的路径几何图形。

这将剪辑图像,以便仅显示您想要的区域,并且剪辑的路径几何形状之外的任何内容都将被“剪辑”

。您可以执行此操作来剪辑图像的任意部分。

这就是您要找的吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文