我在UIImageview中顯示圖像,我想剪裁圖像,以下是我的要求。如何在Monououch中裁剪圖像Xamarin
選擇裁剪圖標應顯示一個固定大小(600X600)的方形,該方形通過網格線固定在圖像上以協助校正圖像。將會有一個控件允許圖像在網格下轉動。
我在UIImageview中顯示圖像,我想剪裁圖像,以下是我的要求。如何在Monououch中裁剪圖像Xamarin
選擇裁剪圖標應顯示一個固定大小(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;
}