How To Clear an Image

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

..NET 2.0

I have an Image on my form and I am trying to clear this image and re-load
it with different image.
Everything works fine, but once I load the image I can not delete this image
until I close the program. How can I clear the image so I can delete it.

Here's my code:

string imgFileName = @"C:\myImage.jpg";
this.pic.Image = Image.FromFile(imgFileName);
//
// clear the image
//
this.pic.Image.Dispose();
this.pic.Image = null;
//
//
//


If I try to delete the image with Windows Explorer before I end the program
I get the following error message

---------------------------
Error Deleting File or Folder
---------------------------
Cannot delete 14036: It is being used by another person or program.

Close any programs that might be using the file and try again.
---------------------------
OK
---------------------------


Thank You


Peter
 
All you did was clear the image variable. That is not related to the image
file on disk.

It's like loading an assembly dynamically. You may be done loading it and
using it, but the executable will have a lock on it until it is done.

I would try manually reading the image as a series of bytes, make sure you
open the stream in readonly mode. Then close it, and create the image from
the bytes (I think there is a method to do that).
 
I had the same problem awhile back and the best way I found was to load it
from a filestream;

using (FileStream fsIn = new FileStream(@"C:\myImage.jpg",, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
pictureBox1.Image = Image.FromStream(fsIn);
}
 
Mel said:
I had the same problem awhile back and the best way I found was to load it
from a filestream;

using (FileStream fsIn = new FileStream(@"C:\myImage.jpg",, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
pictureBox1.Image = Image.FromStream(fsIn);
}


Thank you very much!

This works great!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top