从WPF中的图像裁剪对角线区域 [英] Crop a diagonal area from an image in WPF

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

问题描述

我想在画布上使用用户绘制的矩形从图像裁剪。矩形可以移动,重新调整大小和旋转。

I want to crop from an image using user-drawn rectangles on a canvas. The rectangles can be moved, re-sized, and rotated.

当用户选择获取裁剪图像时,矩形内的区域应保存在第二张图像中页面上的位置,只要矩形不旋转,我就可以完美地完成。 (直接使用CroppedBitmap。)但是,当矩形处于某个角度时,我不知道如何执行裁剪。

When the user selects "Get Cropped Image", the area inside the rectangle should be saved in a second image location on the page, which I can do perfectly well, so long as the rectangle is not rotated. (Straight-forward use of CroppedBitmap.) However, when the rectangle is at an angle I do not know how to perform the crop.

这就是我想要做的(原谅我糟糕的MS Paint技能):

This is what I want to do (forgive my poor MS Paint skills):

我的问题是:

1)如何正确跟踪或计算矩形的点数?

1) How do I correctly track or calculate the points of the rectangle?

和,

2)获得积分后,如何裁剪旋转的矩形?

2) Once I have the points, how do I crop the rotated rectangle?

编辑:
感谢用户 Rotem ,我相信我拥有回答第二个问题。使用从以下答案中修改的代码:答案1 答案2 ,我看到了很好的结果。不幸的是,我仍然无法跟踪矩形的正确位置点,所以我还不能完全测试它。

Thanks to user Rotem, I believe that I have the answer to the second question. Using code modified from the following answers: Answer 1, Answer 2, I am seeing good results. Unfortunately, I am still unable to track the correct location points for the rectangle, so I cannot fully test this as of yet.

public static Bitmap CropRotatedRect(Bitmap source, System.Drawing.Rectangle rect, float angle, bool HighQuality)
 {
    Bitmap result = new Bitmap((int)rect.Width, (int)rect.Height);
    using (Graphics g = Graphics.FromImage(result))
     {
        g.InterpolationMode = HighQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Default;
        using (Matrix mat = new Matrix())
         {
            mat.Translate(-rect.Location.X, -rect.Location.Y);
            mat.RotateAt(-(angle), rect.Location);
            g.Transform = mat;
            g.DrawImage(source, new System.Drawing.Point(0, 0));
         }
     }
    return result;
 }

编辑:
答案第一点比我原先想象的容易得多。你总是可以通过调用 -

The answer to the first point is much easier than I had originally thought. You can always get the top-left corner of the rectangle by calling—

double top = Canvas.GetTop(rect);
double left = Canvas.GetLeft(rect);

然后您可以使用宽度和高度计算其余点 -

You can then calculate the rest of the points using the width and the height—

Point topLeft = new Point(left, top);
Point topRight = new Point(left + rect.Width, top);
Point bottomLeft = new Point(left, top + rect.Height);
Point bottomRight = new Point(left + rect.Width, top + rect.Height);
Point centerPoint = new Point(left + (rect.Width / 2), top + (rect.Height / 2));

如果您的矩形旋转,那么您必须翻译这些点以确定它们真正位于canvas -

If your rectangle is rotated, then you have to translate these points to determine where they truly lie on the canvas—

public Point TranslatePoint(Point center, Point p, double angle)
 { 
    // get the point relative to (0, 0) by subtracting the center of the rotated shape.
    Point relToOrig = new Point(p.X - center.X, p.Y - center.Y);
    double angleInRadians = angle * Math.PI / 180;
    double sinOfA = Math.Sin(angleInRadians);
    double cosOfA = Math.Cos(angleInRadians);
    Point translatedPoint = new Point(relToOrig.X * cosOfA - relToOrig.Y * sinOfA,
                                      relToOrig.X * sinOfA + relToOrig.Y * cosOfA);
    return new Point(translatedPoint.X + center.X, translatedPoint.Y + center.Y);
 }

一旦你能够翻译左上角,就可以使用Rotem了种植方法。您还可以计算矩形其余部分的位置,这样您就可以确定矩形是否在图像的边界内,是否接触边缘,或者您可能想要做的任何其他事情。这个职位。

Once you are able to translate the top-left corner, you can use Rotem's cropping method. You can also calculate the position of the rest of the rectangle, so you are able to determine if the rectangle is within the bounds of the image, if it is touching an edge, or any other thing that you might want to do in regards to the position.

推荐答案

我发现了自己的问题的答案,并在此过程中进行了适当的编辑。请参阅上面的答案。

I discovered the answer to my own question(s), and made the appropriate edits along the way. Please see above for the answer.

这篇关于从WPF中的图像裁剪对角线区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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