裁剪图片,从X,Y获得矩形?

Ben*_*Ben -2 c# points coordinates

我正在尝试从整数x,y坐标中裁剪C#中的图片..我只是无法弄清楚如何获取它?

public void doCroppedImage(int pointX, int pointY)
{
    Rectangle cropRect = //???
}
Run Code Online (Sandbox Code Playgroud)

ant*_*tew 14

您可以使用此代码.它返回裁剪的图像.

public static Bitmap CropImage(Image source, int x,int y,int width,int height)
{
    Rectangle crop = new Rectangle(x, y, width, height);

    var bmp = new Bitmap(crop.Width, crop.Height);
    using (var gr = Graphics.FromImage(bmp))
    {
        gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel);
    }
    return bmp;
} 
Run Code Online (Sandbox Code Playgroud)

但有一点评论,要裁剪图像,您不仅要知道裁剪点的x和y坐标,还要知道裁剪图像的宽度和高度.