2014-12-09 77 views
1

我試圖通過將上傳的流保存到位圖中,處理該位圖並將處理後的位圖保存爲應保存到FTP的新流來調整上傳的圖像的大小夾。上傳的流成功保存爲位圖並正確處理;這只是說,新的處理流呈現爲損壞的圖像時出現問題。這裏是代碼段:將位圖保存到流中

 s = FileUpload1.FileContent; 
     Bitmap bitmap = (Bitmap)Bitmap.FromStream(s); 
     Bitmap newBmp = new Bitmap(250, 250, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
     newBmp.SetResolution(72F, 72F); 
     Graphics newGraphic = Graphics.FromImage(newBmp); 
     newGraphic.Clear(Color.White); 
     newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
     newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
     newGraphic.DrawImage(bitmap, 0, 0, 250, 250); 
     newBmp.Save(MapPath("temp.jpg")); 
     Stream memoryStream = new MemoryStream(); 
     newBmp.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
+0

我會很感激的任何幫助。提前致謝。 – 2014-12-09 12:48:33

+0

「保存在FTP中」是什麼意思?這是一個網絡協議,而不是文件格式。結果是什麼方式腐敗?是否有錯誤發生,或者是結果中可見的問題?等等。 – 2014-12-09 12:52:00

+0

對不起,我的意思是它應該保存在FTP文件夾中。 – 2014-12-09 12:52:55

回答

2

這裏的關鍵是你得到一個空的圖像,它幾乎總是一個不被從頭讀取的流。

將位圖寫入它之後,您需要回溯到流的開始位置。

memoryStream.Seek(0, SeekOrigin.Begin); //go back to start 

否則,當您嘗試稍後保存該流時,它將從結尾讀取流。當位圖寫入流時,它將字節AND附加到位置。以下是MVC中的示例代碼。

Index.cshtml

@{ 
    ViewBag.Title = "Index"; 
} 

@using (Html.BeginForm("UploadImage", "BitmapConvert", FormMethod.Post, new { enctype = "multipart/form-data" })){ 
    <input type="file" name="uploadFile"/> 

    <input type="submit" value="Upload"/> 
} 

BitmapConvertController.cs

using System.Drawing; 
using System.IO; 
using System.Web; 
using System.Web.Mvc; 

namespace MvcSandbox.Controllers 
{ 
    public class BitmapConvertController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult UploadImage(HttpPostedFileBase uploadFile) 
     { 
      var s = uploadFile.InputStream; 

      var bitmap = new Bitmap(s); 

      var resizedBitmap = new Bitmap(250, 250, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
      resizedBitmap.SetResolution(72F, 72F); 

      using (var g = Graphics.FromImage(resizedBitmap)) 
      { 
       g.Clear(Color.White); 
       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
       g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
       g.DrawImage(bitmap, 0, 0, 250, 250); 

       resizedBitmap.Save("C:\\Test\\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 

       using (var memoryStream = new MemoryStream()) 
       { 
        resizedBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg); 

        using (var dest = new FileStream("C:\\Test\\stream.jpg", FileMode.OpenOrCreate)) 
        { 
         memoryStream.Seek(0, SeekOrigin.Begin); //go back to start 
         memoryStream.CopyTo(dest); 
        } 
       } 

      } 

      return RedirectToAction("Index"); 
     } 
    } 
} 
+0

非常感謝verrrrrrry ..問題在於你說的;內存流不是從頭開始讀取的。當我添加memorystream.seek的行時,它的功能就像一個魅力。感謝一百萬次。 – 2014-12-10 12:34:29