Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The (previously) accepted answer to this question is very poor practice. If you <a href="http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx" rel="nofollow noreferrer">read the documentation</a> on <code>System.Drawing.Bitmap</code>, in particular for the overload that creates a bitmap from a file, you will find :</p> <blockquote> <p>The file remains locked until the Bitmap is disposed. </p> </blockquote> <p>in your code you create the bitmap and store it in a local variable but you never dispose of it when you are done. This means your image object has gone out of scope but has not released its lock on the image file you are trying to delete. For all objects that implement <code>IDisposable</code> (like <code>Bitmap</code>) you must dispose of them yourself. See <a href="https://stackoverflow.com/questions/2926869/do-you-need-to-dispose-of-objects-and-set-them-to-null">this question</a> for example (or search for others - this is a very important concept!).</p> <p>To correct the problem properly you simply need to dispose of the image when you are done with it :</p> <pre><code> if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = new Bitmap(ofd.FileName); // create the bitmap string imgName = ofd.SafeFileName; txtImageName.Text = imgName; pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); ofd.RestoreDirectory = true; img.Dispose(); // dispose the bitmap object } </code></pre> <p>Please do not take the advice in the answer below - you should nearly never need to call <code>GC.Collect</code> and if you need to do it to make things work it should be a very strong signal that you are doing something else wrong. </p> <p>Also, if you only want to delete the <em>one</em> file (the bitmap you have displayed) your deletion code is wrong and will delete every file in the directory as well (this is just repeating Adel's point). Further, rather than keep a global <code>OpenFileDialog</code> object alive simply to store the file name, I would suggest getting rid of that and saving just the file info :</p> <pre><code>FileInfo imageFileinfo; //add this //OpenFileDialog ofd = null; Get rid of this private void button1_Click(object sender, EventArgs e) { if (System.IO.Directory.Exists(path)) { OpenFileDialog ofd = new OpenFileDialog(); //make ofd local ofd.InitialDirectory = path; DialogResult dr = new DialogResult(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = new Bitmap(ofd.FileName); imageFileinfo = new FileInfo(ofd.FileName); // save the file name string imgName = ofd.SafeFileName; txtImageName.Text = imgName; pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); ofd.RestoreDirectory = true; img.Dispose(); } ofd.Dispose(); //don't forget to dispose it! } else { return; } } </code></pre> <p>Then in your second button handler you can just delete the one file you are interested in.</p> <pre><code> private void button2_Click(object sender, EventArgs e) { if (!IsFileLocked(imageFileinfo)) { imageFileinfo.Delete(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload