Help and Support
 

powered byLive Search

Image file is locked when you set the PictureBox Image property to a file

Article ID:309482
Last Review:February 12, 2007
Revision:2.1
This article was previously published under Q309482
For a Microsoft Visual C# .NET version of this article, see 311754 (http://support.microsoft.com/kb/311754/EN-US/).
On This Page

SYMPTOMS

When you load a PictureBox control with a picture file, the Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 Integrated Development Environment (IDE) maintains a lock on the file. This occurs when you set the Image property of a PictureBox control to a file manually at design time, or when you use the FromFile method at run time.

Back to the top

RESOLUTION

To work around this problem, use the FileStream object as follows:
Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg", 
     IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
				

Back to the top

STATUS

This behavior is by design.

Back to the top

MORE INFORMATION

Design Time

When you set the Image property of a PictureBox control at design time, the Visual Studio .NET or Visual Studio 2005 IDE locks the picture file. The picture file remains locked even if you reset the Image property or delete the PictureBox control. The only way to unlock the picture file is to close the Visual Studio .NET or Visual Studio 2005 IDE.

Steps to Reproduce Behavior

1.In Visual Studio .NET or in Visual Studio 2005, create a new Visual Basic Windows Application project. Form1 is created by default.
2.Add a PictureBox control to Form1, and set the Image property to a picture file.
3.In Windows Explorer, try to rename the picture file that you used as the Image property in the PictureBox control. You receive a sharing violation error message.
4.In the Properties window, right-click the Image property, and then click Reset. Try to rename the picture file. You receive a sharing violation error message.
5.Delete the PictureBox control from Form1, and then attempt to rename the picture file from Windows Explorer. You receive a sharing violation error message.

Back to the top

Run Time

If you use the Image.FromFile method to load a picture in a PictureBox control, the picture file is locked when you start the application. The picture file remains locked while the application runs. The picture file is locked even if you set the Image property to Nothing at run time.

Steps to Reproduce Behavior

1.In Visual Studio .NET or in Visual Studio 2005, create a new Visual Basic Windows Application project. Form1 is created by default.
2.Add one PictureBox control and one Button control to Form1.
3.Paste the following code in the Click event for the Button control:
' Specify a valid picture file path on your machine below
PictureBox1.Image = Image.FromFile("C:\WINNT\Web\Wallpaper\Fly Away.jpg")
					
4.Press the F5 key to run the application, and then click the Button control to load the picture in the PictureBox control.
5.While the application is running, try to use Windows Explorer to rename the picture file. You receive a sharing violation error message and cannot rename the file.

Back to the top


APPLIES TO
Microsoft Visual Basic 2005 Express Edition
Microsoft Visual Basic .NET 2003 Standard Edition
Microsoft Visual Basic .NET 2002 Standard Edition

Back to the top

Keywords: 
kbtshoot kbvs2005swept kbvs2005applies kbprb KB309482

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.