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;
- file.SaveAs(fileName);
- Imageimg = Image.FromFile(fileName);
- intimgHeight = 100;
- intimgWidth = 100;
- if (img.Width < img.Height)
- {
-
- imgHeight = 100;
- varimgRatio = (float) imgHeight / (float) img.Height;
- imgWidth = Convert.ToInt32(img.Height * imgRatio);
- }
- elseif(img.Height < img.Width)
- {
-
- 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();
- }
- }
In the above method we are checking whether the image is portrait or landscape. Maximum width or height should be 100 pixels so we are getting ratio of actual height and width for 100 pixel size image. "GetThumbnailImage()" function is used to create a thumbnail of an image. It accepts new width, height, GetThumbnailImageAbort delegate and System.IntPtr.Zero parameters. This function returns Image type object. "Save()" function is used to save the thumbnail file into any specific folder. To save the thumbnail we are using actual file name with “_thumb” suffix and it will save the thumbnail into the same folder where we are saving actual image.
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>
- }
In the above view, we are using Html.BeginForm helper and it is pointing Create action method of ImageUpload controller. For HttpPostedFileBase we use enctype = “multipart/form-data” as additional html attribute. Now if we run the code then you will get the bellow output:
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.