[英]How to crop a cross rectangle from an image using c#?
我想获得图像的某些特定部分,因此我正在裁剪图像。 但是,当我想获得与图像不平行的部分时,我会旋转图像并在之后进行裁剪。
我不想旋转图像并裁剪平行矩形。 我想要的是,在不旋转图像的情况下,从图像中裁剪一个具有一定角度的矩形。
有没有办法做到这一点?
我想我不能很好地表达自己。 这就是我想要做的:示例图片。
假设红色的东西是一个矩形 :) 我想从图像中裁剪那个东西。 裁剪后不需要天使化。 这样mj就可以躺下了。
此方法应该执行您的要求。
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;
}
用法(使用您的 MJ 示例):
Bitmap src = new Bitmap("C:\\mjexample.jpg");
Rectangle rect = new Rectangle(272, 5, 100, 350);
Bitmap cropped = CropRotatedRect(src, rect, -42.5f, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.