2015-05-01 60 views
0

我在這裏搜索這裏的幫助,但沒有什麼完全符合我需要的。我有一張上傳的圖片,我希望在將其保存到天藍色之前更改大小。從流更改圖像大小

所以目前我的代碼是:

public ActionResult UserDetails(HttpPostedFileBase photo) 
    {  var inputFile = new Photo() 
      { 
       FileName = photo.FileName,      
       Data =() => photo.InputStream 
      }; 
//then I save to Azure 

我將如何改變photo.InputStream 100倍100像素的例子嗎?

+0

你到目前爲止嘗試過什麼?有大量的在線教程可用於在C#中調整圖像大小。 – Amy

+0

我已經能夠改變使用WebImage的圖像大小,但後來我只有圖像作爲WebImage對象,我不能保存它:Data =()=> WebImageObject .....不起作用 – Jynn

回答

5

這是我如何做到這一點:

byte[] imageBytes; 

//Of course image bytes is set to the bytearray of your image  

using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) 
    { 
     using (Image img = Image.FromStream(ms)) 
     { 
      int h = 100; 
      int w = 100; 

      using (Bitmap b = new Bitmap(img, new Size(w,h))) 
      { 
       using (MemoryStream ms2 = new MemoryStream()) 
       { 
        b.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg); 
        imageBytes = ms2.ToArray(); 
       } 
      } 
     }       
    }  

從那裏,我用一個MemoryStream上傳。我使用blob存儲並使用UploadFromStreamAsync加載到blob。

這是它的基本視圖。 〜乾杯

+0

謝謝,星期一將嘗試這個。 :) – Jynn