0

Deleting an image which is opened in picturebox:

  private void button1_Click_1(object sender, EventArgs e)
  {
      lblimgname.Text = "Right Thumb 1";
      string savepath = @"C:/BrainScript/" + clientname + "/";

      FileStream fs = new FileStream(savepath + clientname + "_" + lblimgname.Text + ".jpg", FileMode.Open, FileAccess.Read);

      Image img = Image.FromStream(fs);
      fs.Close();
      picRightthumb1.Image = img.Clone() as Image;

      img.Dispose();

      File.Delete(savepath + clientname + "_" + lblimgname.Text + ".jpg");
   }

    private Image GetCopyImage(string path)
    {

      using (Image im = Image.FromFile(path))
          {
          Bitmap bm = new Bitmap(im);
          return bm;
          }
    }

I have written the above code to delete the image which is opened in the picturebox but i am getting error "The process is used by another process" pls help me out from this problem.

gustavodidomenico
  • 4,640
  • 1
  • 34
  • 50
  • How do you import this image and deleting it at the same time? – Yehia Elhawary Sep 16 '14 at 11:12
  • see i have two picturebox one for capturing image and saving into one folder and fetching that image from folder and displaying into another picturebox k.and i am updating the image that time i am deleting the image from that folder but its giving process is using another process – user3796682 Sep 16 '14 at 11:16

1 Answers1

1

I used the following code and it works fine:

private void button1_Click(object sender, EventArgs e)
{
   FileStream file = new FileStream("1.jpg", FileMode.Open);
   pictureBox1.Image = Image.FromStream(file);
   file.Close();

   File.Delete("1.jpg");
}
Matin Lotfaliee
  • 1,745
  • 2
  • 21
  • 43