Question Picturebox image without locking file?

stulish

Well-known member
Joined
Jun 6, 2013
Messages
61
Programming Experience
3-5
Hi guys,

i have a picturebox on my program that will load a picture from file every 10 seconds (C:\Picture1.png), i have another program capturing a picture and saving it to the same location, my first app captures fine and saves it, and the second program loads the picture but when the first program tries to overwrite the picture file it says it is being used by another process, with the code below:

VB.NET:
[COLOR=blue]If[/COLOR] [COLOR=#2B91AF]File[/COLOR].Exists(fName) [COLOR=blue]Then[/COLOR]
[COLOR=blue]Dim[/COLOR] bmp [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] [COLOR=#2B91AF]Bitmap[/COLOR](fName)
R1_3.Image = bmp 
[COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]

i tried disposing of the bmp once i had loaded it into the picturebox with:


VB.NET:
[COLOR=blue]If[/COLOR] [COLOR=#2B91AF]File[/COLOR].Exists(fName) [COLOR=blue]Then[/COLOR]
[COLOR=blue]Dim[/COLOR] bmp [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] [COLOR=#2B91AF]Bitmap[/COLOR](fName)
R1_3.Image = bmp
bmp.Dispose()
[COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]

but this just makes the picturebox display a white box with a red cross (diagonal) inside it.

Is there a way to load a bitmap into a picturebox and then release the file??

Thanks

Stu
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,350
Location
Sydney, Australia
Programming Experience
10+
Call the Load method of the PictureBox and pass the file path or set the ImageLocation property to the file path. By releasing the file though, you will not be able to make changes and then save it. That may well not be an issue for you but that's why creating an Image or Bitmap object the usual way does keep the file open.
 
Top Bottom