1

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!

2 Answers 2

4

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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but once I have a Bitmap object, how can I store it to the db? I mean, what's the equivalent of this line: photo.Content = reader.ReadBytes(fichier.ContentLength); But with a Bitmap?
@Papadoc edited my answer. i hope you understood how. marking my answer as answer would be nice :)
better to use using block using (var stream = new MemoryStream(array)) for disposable objects
0

You also approach to ImageResizer as link below: http://www.c-sharpcorner.com/article/image-resize-in-asp-net-mvc-using-image-resizer/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.