Link to home
Start Free TrialLog in
Avatar of Lorna70
Lorna70

asked on

Image resizer required

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?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lorna70
Lorna70

ASKER

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;

                postedFile.SaveAs(fullPath);
                //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
                //o.GetThumbnail().Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

                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
                System.IO.File.Delete(fullPath);


                string strURL = "add_new_pics.aspx";
                Response.Redirect(strURL, false);
            }
            else
            {
                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.
resizeImageClass.txt
Avatar of Lorna70

ASKER

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