0

I am trying download an image from a website and put it in a picturebox.

    // response contains HttpWebResponse of the page where the image is 
    using (Stream inputStream = response.GetResponseStream()) {
                using (Stream outputStream = File.Open(fileName, FileMode.Create)) {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    do {
                        bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        outputStream.Write(buffer, 0, bytesRead);
                    } while (bytesRead != 0);
                }
            }
    response.Close();

After that, the downloaded image is assigned to a PictureBox like such:

    if (imageDownloaded) {
            pictureBox1.Image = Image.FromFile(filePath);
    }

This all works like a charm first time, but the second time I run the code I get System.IO.IOException: "Additional information: The process cannot access the file ...(file path) ... because it is being used by another process.". I have no idea why...

I looked at 4 other threads such as this one, but they were basically stressing out the need to close streams, which I do, so none of them helped me.

Before you recommend to use pictureBox1.Load() I can't because I need the image downloaded for further development.

EDIT 1: I have actually tried to dispose the image by putting pictureBox1.Image = null before the code above is executed. It is still giving me an exception.

Dave Cameron
  • 159
  • 1
  • 3
  • 16
  • possible duplicate of [how to prevent the Image.FromFile() method to lock the file](http://stackoverflow.com/questions/18250848/how-to-prevent-the-image-fromfile-method-to-lock-the-file) – Chris Oct 30 '14 at 21:34
  • Thanks Chris, exactly what I was looking for. And sorry. – Dave Cameron Oct 30 '14 at 21:38

4 Answers4

0

The Image.FromFile docs state:

The file remains locked until the Image is disposed.

So you need to dispose your Image too to be able to overwrite the file.

Try using the Clone method on your image and dispose the original Image.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
0

The thing is related to the Image.FromFile. If we read a documentation, there is a note:

The file remains locked until the Image is disposed.

This pretty much explains the behavior you get.

To resolve this, you might need to create a copy of the image and assign it to a PictureBox .

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

From MSDN:

The file remains locked until the Image is disposed.

This means its the PictureBox that is holding the file open.

You have two options:

  • Dispose of the PictureBox image before writing the new file.

  • Download the file, make a copy of it and load the copy into the PictureBox - this allows you to write over the downloaded file freely.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

I'll be the contrarian here. :)

While cloning/copying the image will resolve the exception, it raises a different question: why are you overwriting to the same file for a new image?

It seems to me that a better approach would be to download subsequent files to different file names (if they are really different files), or to simply reuse the file already downloaded instead of hitting the network again (if it's just a new request for the same file).

Even if you want to repeatedly download the same file (perhaps you expect the actual file contents to change), you can still download to a new name (e.g. append a number to the file name, use a GUID for the local file name, etc.)

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136