C# 如何在monotouch Xamarin中裁剪图像

C# 如何在monotouch Xamarin中裁剪图像,c#,xamarin.ios,xamarin,crop,C#,Xamarin.ios,Xamarin,Crop,我正在UIImageview中显示图像,我想裁剪图像,以下是我的要求 选择裁剪图标应显示一个固定大小(600X600)的正方形,该正方形通过网格线固定在图像上,以帮助校正图像。将有一个控件,允许在网格下旋转图像。您可以尝试制作一个覆盖矩形,以指定要裁剪的内容。提供x和y(如果图像不在左上角,则这些值均为0)、宽度和高度(例如,它们都应为600)。我不知道有什么方法可以让网格线拉直图像。我也不知道旋转图像的方法。但对于直图像,可以使用类似以下内容的方法: private UIImage Crop(

我正在UIImageview中显示图像,我想裁剪图像,以下是我的要求


选择裁剪图标应显示一个固定大小(600X600)的正方形,该正方形通过网格线固定在图像上,以帮助校正图像。将有一个控件,允许在网格下旋转图像。

您可以尝试制作一个覆盖矩形,以指定要裁剪的内容。提供x和y(如果图像不在左上角,则这些值均为0)、宽度和高度(例如,它们都应为600)。我不知道有什么方法可以让网格线拉直图像。我也不知道旋转图像的方法。但对于直图像,可以使用类似以下内容的方法:

private UIImage Crop(UIImage image, int x, int y, int width, int height)
{
    SizeF imgSize = image.Size;

    UIGraphics.BeginImageContext(new SizeF(width, height));
    UIGraphics imgToCrop = UIGraphics.GetCurrentContext();

    RectangleF croppingRectangle = new RectangleF(0, 0, width, height);
    imgToCrop.ClipToRect(croppingRectangle);

    RectangleF drawRectangle = new RectangleF(-x, -y, imgSize.Width, imgSize.Height);

    image.Draw(drawRectangle);
    UIGraphics croppedImg = UIGraphics.GetImageFromCurrentImageContext();

    UIGraphics.EndImageContext();
    return croppedImg;
} 

这就是我用来居中裁剪图像的地方

//Crops an image to even width and height
public UIImage CenterCrop(UIImage originalImage)
{
    // Use smallest side length as crop square length
    double squareLength = Math.Min(originalImage.Size.Width, originalImage.Size.Height);

    nfloat x, y;
    x = (nfloat)((originalImage.Size.Width - squareLength) / 2.0);
    y = (nfloat)((originalImage.Size.Height - squareLength) / 2.0);

    //This Rect defines the coordinates to be used for the crop
    CGRect croppedRect = CGRect.FromLTRB(x, y, x + (nfloat)squareLength, y + (nfloat)squareLength);

    // Center-Crop the image
    UIGraphics.BeginImageContextWithOptions(croppedRect.Size, false, originalImage.CurrentScale);
    originalImage.Draw(new CGPoint(-croppedRect.X, -croppedRect.Y));
    UIImage croppedImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();

    return croppedImage;
}