2014-12-09 62 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