c# - 如何在 monotouch Xamarin 中裁剪图像

标签 c# xamarin.ios xamarin crop

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

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

最佳答案

这就是我最终用来居中裁剪图像的方法。

//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;
}

关于c# - 如何在 monotouch Xamarin 中裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24971499/

相关文章:

c# - Dictionary.Add 方法如何进行 O(1) 摊销?

c# - 使用 EF Core 查询中的方法

xamarin.ios - MonoTouch,NSLog和TestFlightSdk

c# - SQL 服务器 :A network-related or instance-specific error occurred

c# - 系统异常 : 'Cannot create a table without columns, 是否具有公共(public)属性?

c# - 如何在 Xamarin iOS 中绘制文本?

xamarin.forms - Xamarin跨平台蓝牙连接的示例代码

c# - 设置 xamarin toolbaritems 的背景颜色

c# - 使用 Xamarin 抽象 NS* 对象

c# - Azure 移动服务无法在 xamarin.android 上运行