Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Image when done using it

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
0
0
US
Hi
I have a class that downloads an image from a URL and save is on the local computer. What I do is then display the image. Next I would like to delete the image. But I can not seem to do that. Here is my code.

Code:
//Download File
public SaveLogo(string ImageUrl, string ImageTitle)
		{
			_outputFile = @Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\" + ImageTitle + ".jpg";
			try
			{
				StreamReader URL = new StreamReader(System.Net.WebRequest.Create(ImageUrl).GetResponse().GetResponseStream());
				Image.FromStream(URL.BaseStream).Save(_outputFile ,System.Drawing.Imaging.ImageFormat.Jpeg);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
//Do other code
.
.
.
//Delete the file when member is called
public void DeleteImage(string FileName)
		{
			try
			{
				System.IO.File.Delete(FileName);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

I think this code is holding a lock on the file and not releasing it, there for my delete code is not working. Any Idea's?

Thanks
 
Your locking problem is not in the code you provided. I assume you are using Image.FromFile to load the file into an Image type. Image.FromFile does not release the file after reading it.

I suggest you handle the filestream yourself.

using System.IO;
using System.Drawing;
...
FileStream f = new FileStream(_outputFile, FileMode.Open);
Image i = Image.FromStream(f);
pictureBox1.Image = i;
f.Close();


Happy Coding.
 
Could be you just need to call the .Dispose() method of the acquired Image.

Phil
 
Well Im doing this in a web and a windows app. The windows app is using Image.FromStream, but the web app, just sets the source of a image control to the image. That is where I am failing. I do not see a way to release the resources on the web control. How would I release on the web control?

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top