1

I have a PictureBox on my form that shows img.jpg file inside a specific folder. Every second i am refreshing the image on the form by looking at img.jpg file in the folder. When i try to put a new file with the same name to this folder, Windows gives me an error that the file is in use and wants me to close the program in order to copy and rewrite the image. How can i get rid of this error? Can i disable the usage of the image by software? How can i solve this problem?

sanchop22
  • 2,729
  • 12
  • 43
  • 66
  • Have you tried simply lowering your refresh interval to allow the file to be freed up? – OnResolve May 04 '12 at 12:26
  • 1
    You may try to copy the img.jpg to a temprorary location before displaying it with a new name. Then at every second delete the image in the original folder; copy the new image with a new name and change the displayed image path in the picturebox. – daryal May 04 '12 at 12:29

3 Answers3

2

If you are using the method Image.FromFile("path_to_image") then this behavior (locking the file on disk) is by design.

Look at this article on Microsoft Support
Instead look here on StackOverflow for an alternate method to load an image without locking

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
1

Save the file into a local variable on your form and set that to the picture box, and then instead of refreshing the image file each second, check periodically if the date on the file has changed or something like that. And only refresh the image when it actually changed.

cederlof
  • 7,206
  • 4
  • 45
  • 62
0

Or if you want to save implemented behavior, just create a temp copy of the image and use it. When you updating original one, create a new temp image.

besworland
  • 747
  • 2
  • 7
  • 17