[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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?
0
Lorna70
Asked:
Lorna70
  • 2
2 Solutions
 
käµfm³d 👽Commented:
0
 
sarabhaiCommented:
0
 
Lorna70Author Commented:
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
0
 
Lorna70Author Commented:
Sorry Kaufmed - I've re-read your link and it does show how to resize without first saving to disc.  Many thanks :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now