C# “打开文件”对话框使资源保持打开状态

C# “打开文件”对话框使资源保持打开状态,c#,openfiledialog,C#,Openfiledialog,在使用Openfile对话框打开应用程序中的照片后,除非关闭应用程序,否则无法对该文件执行任何操作。我已经将OpenFile对话框放在using语句中,并尝试了各种方法来释放资源,但没有成功。如何释放该进程以避免错误消息“该进程无法访问该文件,因为它正被另一个进程使用?” using (OpenFileDialog GetPhoto = new OpenFileDialog()) { GetPhoto.Filter = "image

在使用Openfile对话框打开应用程序中的照片后,除非关闭应用程序,否则无法对该文件执行任何操作。我已经将OpenFile对话框放在using语句中,并尝试了各种方法来释放资源,但没有成功。如何释放该进程以避免错误消息“该进程无法访问该文件,因为它正被另一个进程使用?”

       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
            }
        }
如文件中所述:

在释放图像之前,文件将保持锁定状态

因此,您可以尝试制作图像的副本,然后释放原始的
图像

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);
        }
    }
}

如果没有帮助,您可以尝试通过
MemoryStream
和以下文档中所述的方法制作副本:

在释放图像之前,文件将保持锁定状态

因此,您可以尝试制作图像的副本,然后释放原始的
图像

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);
        }
    }
}

如果没有帮助,您可以尝试通过
MemoryStream
和方法进行复制:

您的问题不是(OpenFileDialog)您的问题是PictureBox的问题
如果这不起作用,您可以使用加载图像 为加载图像执行此操作

        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);
        }
您的问题不是(OpenFileDialog)您的问题是PictureBox的问题
如果这不起作用,您可以使用加载图像 为加载图像执行此操作

        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);
        }

为什么需要复制图像?我假设只要将using语句与图像一起使用就可以解决问题…@Chrisi
image
是一个类(即引用类型)。当它被分配到
pbPhoto.Image
属性,然后被释放时,访问该属性会导致
ObjectDisposedException
。为什么需要复制该图像?我假设只要将using语句与图像一起使用就可以解决问题…@Chrisi
Image
是一个类(即引用类型)。当它被分配到
pbPhoto.Image
属性,然后被释放时,对该属性的访问会导致
ObjectDisposedException
。谢谢!我感谢您对实际问题的解释。这起到了作用。使用此代码时出现了一个问题。当我尝试将图像保存到SQL DB时,我现在收到了一个错误。我保存图像为MemoryStream stream=new MemoryStream();pbPhoto.image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);byte[]pic=stream.ToArray()如果你需要更多的帮助,这是我的电子邮件amirhoseinadlfar@gmail.comThanks!我感谢您对实际问题的解释。这很有效。使用此代码时出现了问题。当我尝试将映像保存到SQL DB时,我现在收到一个错误。我保存映像的代码是MemoryStream stream=new MemoryStream();pbPhoto.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);byte[]pic=stream.ToArray();Charles如果您需要更多帮助,请发电子邮件给我amirhoseinadlfar@gmail.com