简体   繁体   中英

Resize an image after upload and before saving it into the db

I have a simple web application (ASP.NET MVC 5 C#) which allows users to upload several files (images actually).

Currently it works well, the images are stored into the data base and I can read them later.

But I want to resize the images before saving them into the db, since the user can upload extremely big images.

Here is my controller:

public ActionResult Create(Annonce annonce, IEnumerable<HttpPostedFileBase> photos)
    {
        if (ModelState.IsValid)
        {
            // Read each uploaded files and add if into the collection
            foreach (HttpPostedFileBase fichier in photos)
            {                                                                                   
                if (fichier != null && fichier.ContentLength > 0)
                {
                    // Making a new object
                    var photo = new Photo
                    {
                        FileName = System.IO.Path.GetFileName(fichier.FileName),
                        ContentType = fichier.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(fichier.InputStream))
                    {
                        photo.Content = reader.ReadBytes(fichier.ContentLength);
                    }
                    // Add the current image to the collection
                    annonce.Photos.Add(photo);
                }
            }

            db.Annonces.Add(annonce);
            db.SaveChanges();
            return RedirectToAction("Details", new { id = annonce.ID });
        }

        return View(annonce);
    }

How can I resize my images and still be able to save them into the db? Is-it even possible?

Thanks!

This code will perform a high quality resizing.(means you wont lose very much)

public static Bitmap ResizeImage(Image image, int width, int height)
 {
   var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);

destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

using (var graphics = Graphics.FromImage(destImage))
{
    graphics.CompositingMode = CompositingMode.SourceCopy;
    graphics.CompositingQuality = CompositingQuality.HighQuality;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

    using (var wrapMode = new ImageAttributes())
    {
        wrapMode.SetWrapMode(WrapMode.TileFlipXY);
        graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);
    }
}

return destImage;  
}

Call ResizeImage() and assign it to a bitmap which you'll insert into your database.goodluck

you can convert it to byte array and than store it in your db as byte type

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return  ms.ToArray();
 }

you can do the same but inverted to get it out and display it as image from the DB:

public Image byteArrayToImage(byte[] byteArrayIn)
   {
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
    }

您也可以通过以下链接访问 ImageResizer: http ://www.c-sharpcorner.com/article/image-resize-in-asp-net-mvc-using-image-resizer/

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