WebImage裁剪到正方形

B Z*_*B Z 6 c# asp.net-mvc image asp.net-mvc-3

有谁知道如何使用新的ASP.Net MVC 3 Html Helper WebImage将上传的文件裁剪成正方形.如果可能的话,我想让它居中.在过去的几个小时里,我一直在试图解决这个问题......任何帮助都表示赞赏!

场景非常简单,用户可以上传图像,然后将图像调整为正方形,以便稍后用作网站中的缩略图.

B Z*_*B Z 13

这对我有用,希望为别人节省一些时间......!

private static void CropImage (HttpPostedFileBase sourceImage) {
  var newImage = new WebImage(sourceImage.InputStream);

  var width = newImage.Width;
  var height = newImage.Height;

  if (width > height) {
    var leftRightCrop = (width - height) / 2;
    newImage.Crop(0, leftRightCrop, 0, leftRightCrop);
  }
  else if (height > width) {
    var topBottomCrop = (height - width) / 2;
    newImage.Crop(topBottomCrop, 0, topBottomCrop, 0);
  }

  //do something with cropped image...
  //newImage.GetBytes();
}
Run Code Online (Sandbox Code Playgroud)