1

I’m using WinForms. In my form I have a picturebox. On form load, my program opens an image document into my picturebox from my C:/image directory. The problem is when my program opens that image I cannot go into my C:/image directory and delete this picture because my application is using it. When I go to C:/image directory and try to delete the picture I get this error.

My goal is to have control over the image document that means i have the ability to delete the specific document even if its being used by my application.

Test: I tested if you can delete an image while viewing it at the same time with "Windows Photo Viewer" installed into in my computer, and that application lets you. Windows photo viewer doesn't lock the images. When you delete an image from the directory the image goes away in windows photo viewer as well. I want to accomplish something similar.

Suggested code: I tried implementing this but, i think i'm implementing it incorrectly.

Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
    img = new Bitmap(bmpTemp);
}

enter image description here

Below i provided my code i wrote to load the picture into my picture box.

    private void Form1_Load(object sender, EventArgs e) //When form load you do this:
    { 
        try // Get the tif file from C:\image\ folder
        {
            string path = @"C:\image\";
            string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.

            pictureBox1.Load(filename[0]);


            lblFile.Text = filename[0];
            RefreshImage(); // refreshing and showing the new file
            opened = true; // the files was opened.


                Image img1 = Image.FromFile(lblFile.Text);
                pictureBox1.Image = img1;
                pictureBox1.Width = img1.Width;
                pictureBox1.Height = img1.Height;
                picWidth = pictureBox1.Width;
                picHeight = pictureBox1.Height;
                getRatio();  
        }
        catch (Exception ex)
        {
            MessageBox.Show("No files or " + ex.Message);
        }          
    }
taji01
  • 2,527
  • 8
  • 36
  • 80
  • 3
    FromFile puts a lock on the file. Try opening it from a stream: [Open Image from file, then release lock?](http://stackoverflow.com/q/6576341/719186) – LarsTech Nov 04 '15 at 18:18
  • You need to make a copy of the image before you open it. You could load it into memory (e.g. with a `MemoryStream`) or make a copy of the file on the filesystem (e.g. a temp file). – PC Luddite Nov 04 '15 at 18:18
  • I'm going to try both methods. I don't know which one should be a better approach. I'm still new to programming. If you guys can give an example on how to implement it into my code, I would appreciate it very much @LarsTech – taji01 Nov 04 '15 at 18:36

1 Answers1

2

Make a copy of the image file bits before creating the image:

private void Form1_Load(object sender, EventArgs e) //When form load you do this:
{ 
    try // Get the tif file from C:\image\ folder
    {
        string path = @"C:\image\";
        string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.

        FileInfo fi = new FileInfo(filename[0]);
        byte [] buff = new byte[fi.Length];
        using ( FileStream fs = File.OpenRead(fileToDisplay) )
        {
            fs.Read(buff, 0, (int)fi.Length);      
        }
        MemoryStream ms = new MemoryStream(buff);
        Bitmap img1 = new Bitmap(ms);
        opened = true; // the files was opened.
        pictureBox1.Image = img1;

        pictureBox1.Width = img1.Width;
        pictureBox1.Height = img1.Height;
        picWidth = pictureBox1.Width;
        picHeight = pictureBox1.Height;
        getRatio();  
    }
    catch (Exception ex)
    {
        MessageBox.Show("No files or " + ex.Message);
    }          
}
Chaz
  • 319
  • 1
  • 6