如何移动调整大小和裁剪后的位图?

How to move in a resized and cropped bitmap?

在 .net 核心中,我有一个 returns 随机图像的控制器。假设我有一个笑脸。当我调用 localhost/1000/1000 时,它 returns 一个宽度和高度均为 1000 像素的笑脸图像。

当我调用 localhost/500/1000 时,我裁剪了它:

到目前为止我已经做到了。

        Rectangle rect = new Rectangle(0,0,width,height);

        rect.Intersect(new Rectangle(0, 0, image.Width, image.Height));

        image = ((Bitmap)image).Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);

        var returnedImage = ImageUtils.ToStream(image, System.Drawing.Imaging.ImageFormat.Jpeg);

        return returnedImage;

我不想有一半的笑脸,我想像这样从中心裁剪:

我该怎么做?

你试过吗?

Rectangle rect = new Rectangle(image.Width / 4, 0, width, height);

编辑(添加说明):从图像的第一四分之一处开始矩形使其覆盖您需要的部分,因此如果您想要右半部分,那么您将从 image.Width / 2 开始。