We help IT Professionals succeed at work.

Image resizer required

Lorna70 asked
I already have some code to resize images but it's very clunky and I'm sure it could be written better - e.g it saves the image to the server, then resizes, then deletes the original image.  This has worked fine on sites where only one admin person is uploading images but I'm now starting a project which may potentially have hundreds of users uploading at once. I need advice and was wondering if anyone could recommend something they've used for a site with hundreds of people uploading concurrently which has been successful?
Watch Question

Most Valuable Expert 2011
Top Expert 2015


Thanks guys - I'll look at these links but I think I need to change the method I'm currently using - you see I let a user upload a photo, I then save that photo to disk, then I retrieve it, then do the resizing and save the resized images to disk then finally delete the original.  Is there not a better way to do this?  Modifications to my code would be appreciated.  Here is my code:

    public void Upload(Object s, EventArgs e)
        int picID = 0;
        //get userid from session
        int userId = 1;
        int photoCatId = 0;
        string strCaption = "";
        string strDesc = "";
        //first check if data entry is valid
        if (Page.IsValid)

            string newFileName = "";

            HttpPostedFile postedFile = FileUpload.PostedFile;
            if (IsImageValid(FileUpload.PostedFile))
                filename = Path.GetFileName(postedFile.FileName);
                //Path where file will be uploaded
                string pathToFile = Server.MapPath("../uploaded/Lightbox/");
                filename = getUniqueFilename();
                string fullPath = pathToFile + "\\" + filename;

                //now resize photo

                Response.ContentType = "image/jpeg";
                ResizeImage o = new ResizeImage();

                // Load your image and resize to 500
                o.File = fullPath;

                o.Width = 500;
                o.UsePercentages = false;
                //use next line to print to screen without saving

                newFileName = picID + ".jpg";

                string newFilePath = pathToFile + "\\" + newFileName;
                o.GetThumbnail().Save(newFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                //Now resize to 100
                o.Height = 100;
                o.Width = 0;
                o.UsePercentages = false;
                o.PreserveAspectRatio = true;
                newFileName = picID + "-s.jpg";
                newFilePath = pathToFile + "\\" + newFileName;
                o.GetThumbnail().Save(newFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                //now delete original large image

                string strURL = "add_new_pics.aspx";
                Response.Redirect(strURL, false);
                lblWarnings.Text = "<font color='red'>Error - please check your image name (no spaces) and size (max width/height = 4000px)</font>";


I've attached another file which has the ResizeImage class code.


Sorry Kaufmed - I've re-read your link and it does show how to resize without first saving to disc.  Many thanks :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.