Image.Save gives file not found exception RRS feed

  • Question

  • User1289595697 posted
    In my application, a user can upload an image. If its too large, it will automatically be scaled.
    But when trying to save the new, scaled image, i get an exception saying 'file not found', and then the location of the image I have selected to upload.

    this is the code:

    private void ScaleAndSaveImage(HttpPostedFile file, string path, string name)
          System.Drawing.Image original = System.Drawing.Image.FromFile(file.FileName);
          int width = original.Width, height = original.Height;
          float scale = 1;

          // This will calculate how much the image will have to be scaled
          if (width > height)
                scale = (
    float) width / MAXWIDTH;
                scale = (float) height / MAXHEIGHT;

          // New width and height, scaled
          width = (int)(width / scale);
          height = (
    int)(height / scale);

          // Creating and saving the image
          System.Drawing.Image newImage = new Bitmap(original, new Size(width, height));
          newImage.Save(path + name, System.Drawing.Imaging.ImageFormat.Jpeg);

    The last line is when the error is thrown. The image, however, WILL be uploaded. But in its original size, not scaled. This is what's confusing me the most.

    If anyone can give me any clues, it would be great ;)

    Wednesday, October 12, 2005 1:47 AM

All replies

  • User268616605 posted
    First upload the file without any scale down and immediatly there after, create an Image object of the uploaded image from its server path.Scale down to your desired size and then save the new file. Finally you may like to delete the original file. let me know if it worked.
    Thursday, October 13, 2005 11:48 PM