Microsoft KB Archive/311754

= PRB: Image File Is Locked When You Set the PictureBox Image Property to a File =

Article ID: 311754

Article Last Modified on 7/24/2002

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q311754



For a Microsoft Visual Basic .NET version of this article, see 309482.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.IO



SYMPTOMS
When you load a PictureBox control with a picture file, the Microsoft Visual Studio .NET 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.



RESOLUTION
To work around this problem, use the FileStream object as follows: // Make sure that you have added the System.IO namespace. using System.IO;

// Specify a valid picture file path on your computer. FileStream fs; fs = new FileStream(&quot;C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg&quot;, FileMode.Open, FileAccess.Read); pictureBox1.Image = System.Drawing.Image.FromStream(fs); fs.Close;



STATUS
This behavior is by design.



Design Time
When you set the Image property of a PictureBox control at design time, the Visual Studio .NET 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 IDE.

Steps to Reproduce Behavior

 * 1) In Visual Studio .NET, create a new Visual C# 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 try to rename the picture file from Windows Explorer. You again receive a sharing violation error message.

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
 In Visual Studio .NET, create a new Visual C# Windows Application project. Form1 is created by default. Add one PictureBox control and one Button control to Form1.  Paste the following code in the Click event for the Button control: // Specify a valid picture file path on your computer. pictureBox1.Image = Image.FromFile(&quot;C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg&quot;);  Press F5 to run the application, and then click the Button control to load the picture in the PictureBox control. While the application runs, attempt to use Windows Explorer to rename the picture file. You receive a sharing violation error message.</li></ol>

Keywords: kbprb kbctrl KB311754

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.