2014-01-17 26 views
1

我一直在尋找真棒imageresizing.net工具,併成功地在我的網站上使用URL API。WebMatrix - 使用ImageResizer與圖像上傳

但是,我很想看看託管API,並調整照片上傳的大小。我已經通過構建一個使用WebImage助手一個非常基本的上傳頁面開始了:

@using ImageResizer; 

@{ 
WebImage photo = null; 
var newFileName = ""; 
var imagePath = ""; 

if(IsPost){ 
    photo = WebImage.GetImageFromRequest(); 
    if(photo != null){ 
     newFileName = Guid.NewGuid().ToString() + "_" + 
      Path.GetFileName(photo.FileName); 
     imagePath = @"images\" + newFileName; 

     photo.Save(@"~\" + imagePath); 
    } 
} 
} 

我想在此上傳使用ImageResizing工具,調整大小/作物爲特定的大小。

下面是我需要插入的代碼(我有一個現有的圖像上運行進行了測試,它工作正常):

ImageResizer.ImageBuilder.Current.Build(imagePath,imagePath, 
new ResizeSettings("width=620&height=405&mode=crop")); 

任何想法的房子,我可以合併兩個,而之前調整我救人?

+0

我還抄在這裏找到的建議http://stackoverflow.com/questions/14064563/mvc3-imageresizer但是當我運行頁面,我得到一個錯誤說「源可能只是字符串的一個實例,VirtualFile,IVirtualBitmapFile,HttpPostedFile,HttpPostedFileBase,Bitmap,Image或Stream。 參數名稱:source「 – Gavin5511

+0

你給它一個WebImage實例嗎?不要使用WebImage,[WebImage是邪惡的](http://stackoverflow.com/questions/9710448/asp-net-web-pages-using-webimage-to-resize-and-save-a-photo)。這裏也沒用/毫無意義。直接給ImageResizer上傳。 –

回答

1

你太親近了! Server.MapPath是你正在尋找的缺失部分。

@{ 
    if (IsPost) { 
     var photo = WebImage.GetImageFromRequest(); 
     if (photo != null) { 
      var filename = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName); 
      var localPath = Server.MapPath("~/") + @"images\" + filename; 
      photo.Save(localPath);   
      ImageResizer.ImageBuilder.Current.Build(localPath, localPath, new ImageResizer.ResizeSettings("width=620&height=405&mode=crop")); 
     } 
    } 
} 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>My Site's Title</title> 
     <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
    </head> 
    <body> 
     <form method="post" enctype="multipart/form-data"> 
      <input type="file" name="mahfile"> 
      <input type="submit"> 
     </form> 
    </body> 
</html> 
+0

謝謝兄弟!讓我放下自己,我會放手一搏! – Gavin5511

+0

這是一個絕對的對待賈斯汀。能夠在網頁上擁有這種罕見知識的人真是太好了,但很不幸,這些知識並不多。 – Gavin5511

+0

賈斯汀,只是一個更快的問題......有沒有什麼辦法可以轉換成上傳JPG格式?我試過使用「格式= JPG」,但它仍然使用上傳圖像的格式(例如,如果我上傳的PNG或GIF)? – Gavin5511