从图像中裁剪System.Windows.Shapes矩形

时间:2014-02-03 22:00:35

标签: c# wpf crop cropimage.net

我想从图像中裁剪旋转的矩形。我想做的是这样的事情:

 System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle();
 // rect.RadiusX = ...; // rect.Height = ....; ..
  rect.RenderTransform = new RotateTransform(angle);

然后从Image中裁剪此矩形。 我找到的所有代码都从图像中裁剪出一个System.Windows.Drawing矩形。但是,我需要裁剪一个System.Windows.Shapes来应用不适用于System.Windows.Drawing Rectangle的旋转变换。

1 个答案:

答案 0 :(得分:3)

由于您的问题缺少一些代码。据我所知,给定的方法可以为您提供适当的结果。如果答案不符合您的要求,请详细说明问题陈述。

  public static Bitmap CropRotatedRect(Bitmap source, Rectangle rect, float angle, bool HighQuality)
    {
        Bitmap result = new Bitmap(rect.Width, 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 Point(0, 0));
            }
        }
        return result;
    }

希望有所帮助。