0

After opening a photo in my application using the Openfile Dialog I cannot do anything with the file unless I close my application. I have placed the OpenFile Dialog in a using statement and tried various ways to release resources with no success. How can I release the process to avoid the error message "The process cannot access the file because it is being used by another process?

       using (OpenFileDialog GetPhoto = new OpenFileDialog())
        {
            GetPhoto.Filter = "images | *.jpg";
            if (GetPhoto.ShowDialog() == DialogResult.OK)
            {
                pbPhoto.Image = Image.FromFile(GetPhoto.FileName);
                txtPath.Text = GetPhoto.FileName;
                txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
                //GetPhoto.Dispose();  Tried this
                //GetPhoto.Reset();  Tried this
                //GC.Collect(): Tried this
            }
        }
Charles
  • 53
  • 7

2 Answers2

1

As stated in the doc for Image.FromFile:

The file remains locked until the Image is disposed.

So you could try to make a copy of the image and then release an original Image:

using (OpenFileDialog GetPhoto = new OpenFileDialog())
{
    GetPhoto.Filter = "images | *.jpg";
    if (GetPhoto.ShowDialog() == DialogResult.OK)
    {
        using (var image = Image.FromFile(GetPhoto.FileName))
        {
            pbPhoto.Image = (Image) image.Clone(); // Make a copy
            txtPath.Text = GetPhoto.FileName;
            txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
        }
    }
}

If it not help, you could try to make a copy via the MemoryStream and the Image.FromStream method: System.Drawing.Image to stream C#

Dmitry
  • 13,797
  • 6
  • 32
  • 48
  • Why do you need to copy the image? I would assume just having the using statement with the image would solve the problem... – Chrisi Sep 21 '20 at 14:49
  • @Chrisi `Image` is a class (that is a reference type). When it assigned to `pbPhoto.Image` property and then disposed, access to that property leads to `ObjectDisposedException`. – Dmitry Sep 21 '20 at 14:53
1

Your problem is not (OpenFileDialog) your problem is for PictureBox
you can use this for load image or if this not works do this for load image

        OpenFileDialog GetPhoto = new OpenFileDialog();
        GetPhoto.Filter = "images | *.jpg";
        if (GetPhoto.ShowDialog() == DialogResult.OK)
        {
            FileStream fs = new FileStream(path: GetPhoto.FileName,mode: FileMode.Open);
            Bitmap bitmap = new Bitmap(fs);
            fs.Close(); // End using
            fs.Dispose();
            pbPhoto.Image = bitmap;
            txtPath.Text = GetPhoto.FileName;
            txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
        }
  • Thanks! I appreciate the explanation as to the actual problem. This worked. – Charles Sep 21 '20 at 15:52
  • An issue has come up using this code. When I try to save the image to my SQL DB I now get an error. My code for saving the image is MemoryStream stream = new MemoryStream(); pbPhoto.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] pic = stream.ToArray(); – Charles Sep 21 '20 at 16:39
  • Charles it is my Email if you need more help amirhoseinadlfar@gmail.com – amirhosein adlfar Sep 21 '20 at 16:42