In this article I am going to show you how to create a thumbnail during image upload using MVC. If we create a thumbnail of images then it reduces the image byte size with its dimension. If we create a thumbnail of 700 KB file then thumbnail’s file size would be less than 25 KB. .NET provides Image class and its one method can create a thumbnail but reducing the dimension of image with its ratio is very important.
First of all we will create a Model to store the image path into database. This approach is totally optional, if you don’t want to store the image path into database.
- Public class ImageUploadModels
- {
- [Key]
- Public int ImgageID
- {
- get;
- set;
- }
- [Required]
- Public string ImagePath
- {
- get;
- set;
- }
- }
Now we add “ImageUploadThumnail” action into "HomeController" controller. Below is the snap-shot of “ImageUploadThumnail” action method. In this method we are getting "ImageUploadModels" and "HttpPostedFileBase".
- [HttpPost]
- Public ActionResult ImageUploadThumnail(ImageUploadModels image, HttpPostedFileBase file)
- {
- try {
- if (file != null)
- {
- varfileName = Path.GetFileName(file.FileName);
- varthumbName = fileName.Split('.').ElementAt(0) + "_thumb." + fileName.Split('.').ElementAt(1);
- fileName = Path.Combine(Server.MapPath("/Images"), fileName);
- thumbName = Path.Combine(Server.MapPath("/Images"), thumbName);
- image.ImagePath = fileName; //to store into database, if we use DbContext
- file.SaveAs(fileName);
- Imageimg = Image.FromFile(fileName);
- intimgHeight = 100;
- intimgWidth = 100;
- if (img.Width < img.Height)
- {
- //portrait image
- imgHeight = 100;
- varimgRatio = (float) imgHeight / (float) img.Height;
- imgWidth = Convert.ToInt32(img.Height * imgRatio);
- }
- elseif(img.Height < img.Width)
- {
- //landscape image
- imgWidth = 100;
- varimgRatio = (float) imgWidth / (float) img.Width;
- imgHeight = Convert.ToInt32(img.Height * imgRatio);
- }
- Image thumb = img.GetThumbnailImage(imgWidth, imgHeight, () => false, IntPtr.Zero);
- thumb.Save(thumbName);
- }
- return View();
- } catch (Exception ex)
- {
- ViewBag.Message = ex.Message.ToString();
- return View();
- }
- }
Now we create a view named “ImageUploadThumnail.cshtml” in Home folder of Views. Below is the snap-shot of view.
- @model KnowckoutMvcApp.Models.ImageUploadModels
- @using(Html.BeginForm("ImageUploadThumnail", "Home", null, FormMethod.Post, new {
- enctype = "multipart/form-data"
- })) {
- @Html.AntiForgeryToken()
- @Html.ValidationSummary(true); < fieldset > < legend > Image < /legend> < divclass = "editor-label" > @Html.LabelFor(model => model.ImagePath) < /div> < divclass = "editor-field" > < inputid = "ImagePath"
- title = "Upload an image"
- type = "file"
- name = "file" / > < /div> < p > < inputtype = "submit"
- value = "Upload" / > < /p> < /fieldset>
- }

If we upload any large image then it will store the image in Image folder of solution and will create a new file with “_thumb” suffix, which will be a thumbnail of actual image. Below is the image of Image folder.



Jaime StuardoPosted Oct 21, 2019, 6:21 PM
Hello, I am tryng to do it, but I need to do another step at the end of the action. I need to rename the file to add a prefix corresponding toan ID of a table. The fact is that the uploaded file is locked by iisexpress. The locking occurs until I shutdown iisexpress. The thumnail image is not locked. Is there a way to solve this?
Santosh Kumar AdidawarpuPosted May 8, 2018, 12:29 PM
Hello Neeraj Kale, thanks for your feedback. It is a sample code, we can customize it as per our need. My code snippet is just to give you an idea where you can extend it as per the need. Thank you :).
Neeraj KalePosted May 8, 2018, 7:59 AM
Hi,There is small bug in above code, in scenario where img width is smaller than height while creating thumbnail .Need to divide width by height.