Upload Image with Resize

anil kumar 61 Reputation points
2022-03-05T09:36:42.207+00:00

I am uploading image in folder using below method .In this I want to resize it before upload
have any idea with the same
Thanks In advance

public string FileUpload(OffViewModel vm)
{

            string filename = null;
            if (vm != null)
            {
            try
            {

                string uploaddir = Path.Combine(webHostEnvironment.WebRootPath, "images");
                // filename = Guid.NewGuid().ToString() + "-" + vm.ImagePath.FileName;
                string extension = Path.GetExtension(vm.ImagePath.FileName);
                filename =  id+ extension;
                string filePath = Path.Combine(uploaddir, filename);

                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }

                using (var filestream = new FileStream(filePath, FileMode.Create))
                {

                    vm.ImagePath.CopyTo(filestream);
                }
            }
            catch (Exception ex)
            {

            }
           }
            return filename;
        }
Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. satya karki 996 Reputation points MVP
    2022-03-07T01:11:21.303+00:00

    HI @anil kumar

    You can use the below method to resize the image.

    private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)    
    {    
        //Get the image current width    
        int sourceWidth = imgToResize.Width;    
        //Get the image current height    
        int sourceHeight = imgToResize.Height;    
        float nPercent = 0;    
        float nPercentW = 0;    
        float nPercentH = 0;    
        //Calulate  width with new desired size    
        nPercentW = ((float)size.Width / (float)sourceWidth);    
        //Calculate height with new desired size    
        nPercentH = ((float)size.Height / (float)sourceHeight);    
        if (nPercentH < nPercentW)    
            nPercent = nPercentH;    
        else    
         nPercent = nPercentW;    
         //New Width    
         int destWidth = (int)(sourceWidth * nPercent);    
         //New Height    
         int destHeight = (int)(sourceHeight * nPercent);    
         Bitmap b = new Bitmap(destWidth, destHeight);    
         Graphics g = Graphics.FromImage((System.Drawing.Image)b);    
         g.InterpolationMode = InterpolationMode.HighQualityBicubic;    
         // Draw image with new width and height    
         g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);    
         g.Dispose();    
         return (System.Drawing.Image)b;    
    }  
    

    To implement.

     System.Drawing.Image img = System.Drawing.Image.FromFile(filePath);  
        Bitmap b = new Bitmap(img);   
        System.Drawing.Image i = resizeImage(b, new Size(100, 100));  
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rijwan Ansari 766 Reputation points MVP
    2022-03-07T01:21:33.667+00:00

    HI @anil kumar

    You can scale the image before uploading as shown below.

    public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)  
        {  
            var ratio = (double)maxHeight / image.Height;  
            var newWidth = (int)(image.Width * ratio);  
            var newHeight = (int)(image.Height * ratio);  
            var newImage = new Bitmap(newWidth, newHeight);  
            using (var g = Graphics.FromImage(newImage))  
            {  
                g.DrawImage(image, 0, 0, newWidth, newHeight);  
            }  
            return newImage;  
        }  
    

    Using above function in your file method.

    using (var filestream = new FileStream(filePath, FileMode.Create))  
                     {  
                         System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(filestream);  
                         System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81);//adjust size as per your requirement  
                              
                         vm.ImagePath.CopyTo(filestream);  
                     }  
    

  2. anil kumar 61 Reputation points
    2023-03-09T09:19:37.36+00:00

    Hi @Ayhan YILDIZ

    Yes

    I have just used few line of coding to resize image rest code is same as I have posted in my question.happy to share these line of code.

    In this I have used nuget pacakage -->SixLabors.ImageSharp

    using(var image= SixLabors.ImageSharp.Image.Load(vm.ImagePath.OpenReadStream()))
                        {
                            string newsize = ImageResize(image, 150, 150);
                            string[] sizearray = newsize.Split(',');
                            image.Mutate(m => m.Resize(Convert.ToInt32(sizearray[1]), Convert.ToInt32(sizearray[0])));
                            image.Save(filePath);
                        }
    
    
    Happy Coding
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.