Image resizer required

Posted on 2012-08-13
Last Modified: 2012-08-23
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?
Question by:Lorna70
    LVL 74

    Accepted Solution

    LVL 9

    Assisted Solution


    Author Comment

    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.

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now