裁剪图像的选定部分 [英] Cropping a selected portion of an Image

查看:125
本文介绍了裁剪图像的选定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序中使用以下XAML以在图像后面绘制折线形状。



I'm using the following XAML in a program in order to draw polyline shape behind an image.

<Polyline x:Name="line" Stroke="Black" StrokeThickness="1" Points="{Binding ElementName=Control1,Path=pointCollection,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />





以下是在鼠标点击事件中绘制折线形状的代码:





The following is the code behind to draw the polyline shape in mouse click event:

var mousePosition = e.GetPosition(sender as UIElement);
                line.StrokeThickness = 1;
                line.Fill = new SolidColorBrush(Colors.Transparent);
                if (pointCollection.Count > 0 && (pointCollection[0].X + 3 > mousePosition.X && pointCollection[0].X - 3 <= mousePosition.X) && (pointCollection[0].Y + 3 > mousePosition.Y && pointCollection[0].Y - 3 <= mousePosition.Y))
                {
                    isCompleted = true;
                    mousePosition.X = pointCollection[0].X;
                }
                if (isMouseMoveAdded)
                {
                    pointCollection.RemoveAt(pointCollection.Count - 1);
                    isMouseMoveAdded = false;
                }
                pointCollection.Add(mousePosition);
                ptsdisplay.ItemsSource = null;
                ptsdisplay.ItemsSource = pointCollection;







我想知道如何裁剪带有所选部分的图像(折线形状)。



提前致谢。




I would like to know how I can crop the image with the selected portion (Polyline Shape).

Thanks in advance.

推荐答案

尝试这样做:



1.将折线插入< window.resources>

2.在图像控件中,绑定剪辑使用DynamicResource的折线属性

3.如果要保存图像,可以将图像放入画布并正确设置宽度和高度(图像的w和h),顶部和左侧将属性附加到0并按照本教程保存png。您可以写一篇文章来将png的背景更改为透明。 ( http://stackoverflow.com/questions/8881865/saving-a-wpf -canvas-as-a-image [ ^ ])。



希望它有所帮助。



Jymmy097



编辑:很明显,如果你不需要保存图像,请不要按照第三点
Try to do this way:

1. Insert your polyline into <window.resources>
2. In the image control, bind the clip property to the polyline with DynamicResource
3. If you want to save the image, you might put your image into a canvas and set width and height properly (w and h of the image), top and left attached properties to 0 and follow this tutorial to save a png. You might write an article to change the background of the png to transparent. (http://stackoverflow.com/questions/8881865/saving-a-wpf-canvas-as-an-image[^]).

Hope it helped.

Jymmy097

Edit : obiviously, if you don't need to save the image, don't follow the third point


除非选择了所有图像,否则无法裁剪选区,这会创建一个空图像。不是很明显吗?您只能裁剪出未选择的图像部分。也许你只是不理解作物一词,需要一些你无法解释的东西。 裁剪意味着去除图像的一些外部部分,边缘处有一些矩形框状区域。这很简单。



其他操作都是未定义的。如果你能确定你想要达到的目标,我将很乐意帮助你。您无法从图像中删除某些任意形状。这是什么意思?例如,您可以使此部分透明,但它始终是相同图像的一部分。 根据定义,图像是矩形的。您可以创建非矩形形状的UI元素(再次,轻松),但它不是图像,它可以是UI元素。



-SA
It's impossible to crop a selection, unless all image is selected, which would create an empty image. Isn't it obvious? You can only crop out unselected part of the image. Perhaps you just don't understand the term "crop" and needs something else which you fail to explain. "Crop" means removing some outer part of image, some rectangular frame-like area at its edges. This is easy.

Other operations are simply undefined. If you can define what you want to achieve, exactly, I will gladly help. You cannot "remove" some arbitrary shape from an image. What would it mean? For example, you can make this part transparent, but it will always remain the part of the same images. Images, by definition, are rectangular. You can create a UI element of non-rectangular shape (again, easily), but it won't be an "image", it could be a UI element.

—SA


这篇关于裁剪图像的选定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆