如何使用 PathGeometry 作为 BitmapSource(或任何图像数据)的掩码?
假设我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
好的,那么我的例子应该可以工作了。根据您的情况,它可能不是性能最好的,但这将是一个起点。
它看起来像这样。显然,剪辑中的要点会因您的情况而有所不同,但您明白了。
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.
不知道你所说的“剪掉”某些部分是什么意思。这可能意味着您只需要显示图像的区域,或者您需要实际创建仅剪切部分或不同内容的新图像?
我的第一个想法是最简单的解决方案,只需将 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?