Question

From an answer in a previous question, given by Hans Passant I understood that :

 MyImage = new Bitmap(openFileDialog1.FileName);
 pictureBox1.Image = (Image)MyImage;

Yes, that code puts a lock on the file. The lock is produced by a memory mapped file object that GDI+ creates to efficiently map the pixel data of the file into memory without having to allocate space in the paging file. You will not be able to delete the file as long as the image is displayed in the picture box and not disposed, the lock prevents that.

The question was about an error(sometimes) when I tried to delete a previously used image from the PictureBox. It seems that in this exact situation even calling:

if (MyImage != null)
{
    MyImage.Dispose();
}

doesn't guarantee that when the time comes to delete the image(i guess this could be any kind of unmanaged resource) it will be disposed.

So I have two questions - the first is related to my current problem with using BitMap in the PictureBox and the locking of the resource caused by that. I saw that this is not the only way to show an image inside the PictureBox so is there another way which won't cause this problem and will guarantee that the image is released to be deleted anytime I need to.

And my second question - what is the basic approach when I have to work with unmanaged resources in .NET and I need to do operations like Delete with them. I guess this problem may come up when working with any kind of unmanaged resource in .NET, is there a common approach to solve the problem with the locking/releasing the resource in a certain moment and not relaying on the Garbage Collector (which obviously will fail you from time to time since it may take different time for releasing). I saw that some use GC... methods to call explicitly finilizer and stuff like that but a college mention that it's better to not use those methods since they may cause unpredictable problems.

So any guidelines, suggestions?

Was it helpful?

Solution

The "common approach" to what you describe is IDisposable and the Dispose() method, most commonly exposed via using. If that isn't working in a specific example, then there may be a coding bug in that specific example. That bug doesn't change the common approach.

Re that one example; try loading it in memory instead:

var data = File.ReadAllBytes(path);
var ms = new MemoryStream(data);
MyImage = Bitmap.FromStream(ms);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top