locked
Image resize RRS feed

  • Question

  • User-1564758913 posted

    Hi, I have made a page that generates thumbnails from a larger image.

    I works but the images load really slow. How can I speed this up?

     

    public partial class MakeThumbnail : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                
                string file = Request.QueryString["file"];
                double dWidth, dHeight;
                int intHeight = int.Parse(Request.QueryString["maxSize"]);
                int intWidth = intHeight;  //Bilden ska passas in i en kvadrat på maxSize*maxSize pixlar
    
                try
                {
                    System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(Server.MapPath(file));
    
                    //Räkna om antingen höjd eller bredd, utifrån proportionerna på orginalbilden
    
                    if (sourceImage.Size.Width < sourceImage.Size.Height)
                    {
                        dWidth = intWidth * (Convert.ToDouble(sourceImage.Size.Width) / Convert.ToDouble(sourceImage.Size.Height));
                        intWidth = Convert.ToInt32((Math.Round(dWidth, 0)));
                    }
                    else
                    {
                        dHeight = intHeight * (Convert.ToDouble(sourceImage.Size.Height) / Convert.ToDouble(sourceImage.Size.Width));
                        intHeight = Convert.ToInt32((Math.Round(dHeight, 0)));
                    }
    
                    // create an image object, using the filename we just retrieved
                    System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file));
    
                    // create the actual thumbnail image
                    System.Drawing.Image thumbnailImage = image.GetThumbnailImage(intWidth, intHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
    
                    // make a memory stream to work with the image bytes
                    MemoryStream imageStream = new MemoryStream();
    
                    // put the image into the memory stream
                    thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                    // make byte array the same size as the image
                    byte[] imageContent = new Byte[imageStream.Length];
    
                    // rewind the memory stream
                    imageStream.Position = 0;
    
                    // load the byte array with the image
                    imageStream.Read(imageContent, 0, (int)imageStream.Length);
    
                    // return byte array to caller with image type
                    Response.ContentType = "image/jpeg";
                    Response.BinaryWrite(imageContent);
                }
    
                catch { }
            }    
    
            public bool ThumbnailCallback()
            {
                return true;
            }
        }


     

     

      

    Monday, October 19, 2009 8:49 AM

Answers

  • User-263151482 posted

    You could use the Application/Session/Cache variable the save the thumbnails, if they are used repeatedly then only the first load takes some time and the other one's are really fast.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 19, 2009 11:26 AM

All replies

  • User-263151482 posted

    You could use the Application/Session/Cache variable the save the thumbnails, if they are used repeatedly then only the first load takes some time and the other one's are really fast.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 19, 2009 11:26 AM
  • User494352855 posted

    Hi, there are several reasons why your code is running slow. 

    1) No caching. Image resizing is a CPU-heavy operation that will crash your site under heavy load - unless you have disk caching.

    2) Your Jpegs are being saved at 100 percent quality, resulting in uneccesarily large file sizes. You can set this to 90 and get much smaller files without perceptible loss.

    3) You will experience a memory leak with this code if it runs for a long time - any errors occuring during the resize will result in open/locked files and unclaimable memory. You must use the using() keyword to prevent this.

    Take a look at this list of pitfalls when resizing images in ASP.NET for more details on how to do it properly..

    There is also a commercial image resizer available if you'd rather use an off-the-shelf module that is well tested.

    Monday, October 26, 2009 11:51 AM