• 0

[VB.NET or C#] Replace file won't work


Question

I have a picturebox that loads an image, and I need to let the user replace the image. How can I replace the file of the image that was loaded into the picturebox? I tried settings the picturebox's image to "New Bitmap(1,1)" to clear it first, but it didn't help.

Help please?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Try PictureBox.Load( string url);

If the url parameter indicates a file, the recommended format is file:///[path using forward slashes]. For example, an image file named myPicture.jpglocated atc:\ would be accessed by passing "file:///c:/myPicture.jpg"

from http://msdn.microsoft.com/en-us/library/f6ak7was.aspx

My guess is that you are using "PictureBox.Image = new Bitmap(filePath")" and the Bitmap object is still using the file so that why you get the "still in use" error.

There might be a better way, I havent played with C# for a while.

Edited by Doli
Link to comment
Share on other sites

  • 0

The file is probably locked. You need to load the resource from a stream so that it can be released once it is set in the picturebox. Then, overwriting the file should work.

using (FileStream imgStrm = new FileStream("C:\\test.jpg", FileMode.Open, FileAccess.Read))
{			
	myPictureBox.Image = Image.FromStream(imgStrm);
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.