如何裁剪使用C#的图像的跨矩形? [英] How to crop a cross rectangle from an image using c#?

查看:256
本文介绍了如何裁剪使用C#的图像的跨矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个图像的某些特定部位,所以我裁剪图像。然而,当我想要得到不平行于图像的一部分,我旋转图像和作物之后。

I want to get some specific parts of an image so I'm cropping the images. However, when I want to get a part that is not parallel to the image, I rotate the image and crop afterwards.

我不想旋转图像和裁剪平行矩形。我要的是,没有旋转图像,裁剪的矩形从图像的角度。

I don't want to rotate the image and crop a parallel rectangle. What I want is, without rotating the image, to crop a rectangle with an angle from the image.

有没有办法做到这一点?

Is there any way to do that?

我想我不能表达自己不够好。这就是我想做的事:例如图片

I think I couldnt express myself well enough. This is what I want to do: example picture.

假设红色的东西是一个矩形:)我要裁剪那个​​东西出来的形象。裁剪后不需要进行angeled。所以MJ可以躺下来。

Assume the red thing is a rectangle :) I want to crop that thing out of image. After cropping it doesn't need to be angeled. So mj can lie down.

推荐答案

此方法应该执行你问什么。

This method should perform what you asked for.

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为例):

usage (using your MJ example):

Bitmap src = new Bitmap("C:\\mjexample.jpg");
Rectangle rect = new Rectangle(272, 5, 100, 350);
Bitmap cropped = cropRotatedRect(src, rect, -42.5f, true);

这篇关于如何裁剪使用C#的图像的跨矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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