locked
How to resize an uploaded image RRS feed

  • Question

  • User1991482975 posted

    Hi, i would love to resize my image, but i don't know if is possible to do this with no fixed value like the example :

    WebImage img = new WebImage(file.InputStream);
    if (img.Width > 1000)
    img.Resize(1000, 1000);
    img.Save("path");
    return View();

    But make the image under 1,5 MB with proportion, without whanging the hight and lenght..

    For example; i found this whay to cheka how muche is big the photo :

    var sizeMb = (file.ContentLength / 1024f) / 1024f; 

    With this code, do i get the size in MB?

    AND AFTER..

    But how to resize it? kepping same hight and lenght but lower MB dimension (max 1,5 MB)

    Sunday, September 28, 2014 6:46 AM

Answers

  • User281315223 posted

    It sounds like you don't actually need to physically resize the image (as you want to maintain the same size for it), but instead you want to losslessly compress it. Loseless compression will attempt to optimize the image and reduce it's size without actually changing the size of the image. Additionally, you could even likely use lossy compression and set the quality to a percentage like 70-90% in order to see how the size differs.

    These functions are generally not going to be best implemented using built-in functions and you'll be better off using a library. I would recommend trying the ImageMagick library out, which I believe is available via a NuGet package. Basically, just install this package to your Project and then use it as follows :

    // Open your image using the ImageMagick library (after adding it through NuGet)
    MagickImage img = new MagickImage(file.InputStream);
    // Adjust your width, if necessary
    if(img.Width > 1000)
    {
         img.Resize(1000, 1000);
    }
    // Attempt to resize the size (adjust this to get an idea of how it works)
    img.Quality = 60; // Default is 75
    img.Write("path");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 28, 2014 9:06 AM

All replies

  • User281315223 posted

    It sounds like you don't actually need to physically resize the image (as you want to maintain the same size for it), but instead you want to losslessly compress it. Loseless compression will attempt to optimize the image and reduce it's size without actually changing the size of the image. Additionally, you could even likely use lossy compression and set the quality to a percentage like 70-90% in order to see how the size differs.

    These functions are generally not going to be best implemented using built-in functions and you'll be better off using a library. I would recommend trying the ImageMagick library out, which I believe is available via a NuGet package. Basically, just install this package to your Project and then use it as follows :

    // Open your image using the ImageMagick library (after adding it through NuGet)
    MagickImage img = new MagickImage(file.InputStream);
    // Adjust your width, if necessary
    if(img.Width > 1000)
    {
         img.Resize(1000, 1000);
    }
    // Attempt to resize the size (adjust this to get an idea of how it works)
    img.Quality = 60; // Default is 75
    img.Write("path");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 28, 2014 9:06 AM
  • User-2110585397 posted

    Hi,

    For you post, please try the following code, there are 4 kinds of resize.

    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
      {
          Image originalImage = Image.FromFile(originalImagePath);
           
          int towidth = width;
          int toheight = height;
       
          int x = 0;
          int y = 0;
          int ow = originalImage.Width;
          int oh = originalImage.Height;        
     
          switch (mode)
          {        
              case "HW":            
                  break;
              case "W":                   
                  toheight = originalImage.Height * width/originalImage.Width;
                  break;
              case "H":
                  towidth = originalImage.Width * height/originalImage.Height;                    
                  break;        
              case "Cut":              
                  if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
                  {
                      oh = originalImage.Height;
                      ow = originalImage.Height*towidth/toheight;
                      y = 0;
                      x = (originalImage.Width - ow)/2;
                  }
                  else
                  {
                      ow = originalImage.Width;
                      oh = originalImage.Width*height/towidth;
                      x = 0;
                      y = (originalImage.Height - oh)/2;
                  }
                  break;                    
              default :
                  break;
          }    
           
          Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
     
          Graphics g = System.Drawing.Graphics.FromImage(bitmap);
     
          g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
     
          g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     
          g.Clear(Color.Transparent);        
     
          g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), 
              new Rectangle(x, y, ow,oh),
              GraphicsUnit.Pixel);
     
          try
          {            
              bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
          }
          catch(System.Exception e)
          {
              throw e;
          }
          finally
          {
              originalImage.Dispose();
              bitmap.Dispose();                        
              g.Dispose();
          }
      }

    Hope this can be helpful to you.

    Sherwin Zhao
    Best Regards

    Friday, March 6, 2015 4:26 AM
  • User1508394307 posted

    Sherwin, I don't think your post answers the original question. The question was not how to resize in dimension but how to result to smaller size in MB.

    Friday, March 6, 2015 5:40 AM