2013-06-13 52 views
14

我開發了一個使用asp.net mvc4和剃鬚刀的web應用程序。在我的應用程序中有一個文件上傳控件來上傳圖像並保存在臨時位置。如何調整和保存在c#中使用文件上傳控件上傳的圖像

保存圖像應重新調整到特定大小,然後保存在給定的臨時位置。

這裏是我在控制器類中使用的代碼。

public class FileUploadController : Controller 
{ 
    // 
    // GET: /FileUpload/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 
    public ActionResult FileUpload() 
    { 
     return View(); 
    } 

    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult FileUpload(HttpPostedFileBase uploadFile) 
    { 
     if (uploadFile.ContentLength > 0) 
     { 
      string relativePath = "~/img/" + Path.GetFileName(uploadFile.FileName); 
      string physicalPath = Server.MapPath(relativePath); 


      FileUploadModel.ResizeAndSave(relativePath, uploadFile.FileName, uploadFile.InputStream, uploadFile.ContentLength, true); 

      return View((object)relativePath); 
     } 
     return View(); 
    } 
} 

,這裏是在模型類

public class FileUploadModel 
{ 
    [Required] 
    public HttpPostedFileWrapper ImageUploaded { get; set; } 

    public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare) 
    { 
     int newWidth; 
     int newHeight; 
     Image image = Image.FromStream(imageBuffer); 
     int oldWidth = image.Width; 
     int oldHeight = image.Height; 
     Bitmap newImage; 
     if (makeItSquare) 
     { 
      int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth; 
      double coeficient = maxSideSize/(double)smallerSide; 
      newWidth = Convert.ToInt32(coeficient * oldWidth); 
      newHeight = Convert.ToInt32(coeficient * oldHeight); 
      Bitmap tempImage = new Bitmap(image, newWidth, newHeight); 
      int cropX = (newWidth - maxSideSize)/2; 
      int cropY = (newHeight - maxSideSize)/2; 
      newImage = new Bitmap(maxSideSize, maxSideSize); 
      Graphics tempGraphic = Graphics.FromImage(newImage); 
      tempGraphic.SmoothingMode = SmoothingMode.AntiAlias; 
      tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality; 
      tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel); 
     } 
     else 
     { 
      int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight; 

      if (maxSide > maxSideSize) 
      { 
       double coeficient = maxSideSize/(double)maxSide; 
       newWidth = Convert.ToInt32(coeficient * oldWidth); 
       newHeight = Convert.ToInt32(coeficient * oldHeight); 
      } 
      else 
      { 
       newWidth = oldWidth; 
       newHeight = oldHeight; 
      } 
      newImage = new Bitmap(image, newWidth, newHeight); 
     } 
     newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg); 
     image.Dispose(); 
     newImage.Dispose(); 
    } 
} 

使用的代碼,但是當我運行該應用程序它發生的ArgumentException

它說:「參數無效」在下面的代碼行

Bitmap tempImage = new Bitmap(image, newWidth, newHeight); 

我如何在這裏通過有效和適當的參數

public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare) 

回答

54

它很難理解這是什麼問題與您的代碼。但可能你想用另一種方式。您需要添加對System.Web.Helpers命名空間的引用並嘗試以下代碼。

[HttpPost] 
    public ActionResult Index(HttpPostedFileBase file) 
    { 
     WebImage img = new WebImage(file.InputStream); 
     if (img.Width > 1000) 
      img.Resize(1000, 1000); 
     img.Save("path"); 
     return View(); 
    } 

而且此類支持裁剪,翻轉,水印等操作。

+0

根本真棒!謝謝! – MRFerocius

+0

嗨..感謝上傳的post.but你的代碼工作,如果圖像大小不到1 MB ..如果我上傳3 MB或2 MB的圖像大小比它給出錯誤信息「內存不足」。請你幫忙我... – user3217843

+0

謝謝!正是我在找的東西。 –

相關問題