简体   繁体   中英

Saving a bitmap into a stream

I'm trying to resize the uploaded images in ASP.NET by saving the uploaded stream into a bitmap, processing this bitmap and saving the processed bitmap to a new stream that should be saved to an FTP folder. The uploaded stream is successfully saved as a bitmap and processed properly; it's just that there's something wrong with the new processed stream which renders into a corrupted image. Here is the segment of code:

        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);

The key here is that you are getting an empty image, which is almost always a stream not being read from the beginning.

You need to seek back to the start of the stream after you write your bitmap to it.

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

Otherwise, when you try to save off that stream later, it is reading the stream from the end. When the bitmap writes to the stream, it appends the bytes AND it advances the position. Here is sample code in MVC that works.

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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM