Image still in use when trying to delete it RRS feed

  • Question

  • User-884152108 posted


    I have the following code to resize an image and then save the resized image...

    // Save the image from the form input

    // Load the image
    System.Drawing.Bitmap bmpOriginal;
    bmpOriginal =
    new System.Drawing.Bitmap(Server.MapPath("../images/Photos/temp.jpg"));
    Image MyImage = bmpOriginal;

    // Resize the image
    if(MyImage.Height > 100)
       double shtWidth = (MyImage.Width / (MyImage.Height / 100));
       bmpOriginal =
    new System.Drawing.Bitmap(MyImage, Convert.ToInt32(shtWidth), 100);
       MyImage = bmpOriginal;

    // Save the image, then delete the temp file
    "../images/Photos/" + txtName.Text + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);

    The very last line, that deletes the temp image, always causes an exception because the file temp.jpg is still in use. There are two ways I can see of getting around this issue. The one I would like to use is loading the image directly into the image object from the form input without saving it as a temp file first. Is this at all possible?

    Failing that, how could I free up the temp.jpg image so that I can delete it?



    Friday, March 31, 2006 8:07 AM

All replies

  • User-177161035 posted

    I'm having this >>exact<< same issue.

    My idea was to set a process on the thread, that ran every 12 hours to clean out images, until a friend pointed out that this could be bad if someone wanted to do a DOS by uploading TONs of images.. etc. etc.


    Anyone have any ideas how to deal with this? I thought the Dispose() method was supposed to release the processes attached to the object?



    Wednesday, April 12, 2006 4:55 PM
  • User-884152108 posted

    Got it. No idea why I didn't see this before... it's so simple!

    The image can be loaded directly into the Bitmap object using a stream:

    bmpOriginal = new System.Drawing.Bitmap(fuImage.FileContent);

    That way, you don't need to save a temp file.

    Wednesday, April 12, 2006 5:35 PM
  • User-177161035 posted
    Thanks for posting. This solved it for me as well, a much cleaner solution.
    Tuesday, April 18, 2006 3:10 PM