Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Does anyone know how to use the new ASP.Net MVC 3 Html Helper WebImage to crop an uploaded file into a square. I would like to have it centered if possible. I've been banging my head for the last few hours trying to figure this out...any help is appreciated!

The scenario is pretty simple, user can upload an image, the image will then be resized to a square to be used later as a thumbnail in the site.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
469 views
Welcome To Ask or Share your Answers For Others

1 Answer

This worked for me, hope saves some time for others...!

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...