WebImage裁剪到广场 [英] WebImage Crop To Square

查看:128
本文介绍了WebImage裁剪到广场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用新的ASP.Net MVC 3 HTML辅助WebImage裁剪上传的文件转换成一个正方形。我想有它为中心,如果可能的。我一直在敲打我的头,试图弄清楚这一点在最后几个小时...任何帮助是AP preciated!

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!

该方案是pretty简单,用户可以上传一张图片,图片将被调整为一个正方形稍后用作网站的缩略图。

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.

推荐答案

这为我工作,希望可以节省一些时间其他...!

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

这篇关于WebImage裁剪到广场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆