我尝试删除图片框中使用的图像时出现错误 [英] error when i'm trying to delete an image used in a picturebox

查看:58
本文介绍了我尝试删除图片框中使用的图像时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2013创建一个C#Windows应用程序,但遇到了这个错误.

I'm using visual studio 2013 to create a c# windows application and I'm facing this error.

当我尝试删除在图片框中使用的图像时,会出现此错误消息.:

When I'm trying to delete an image that I have been using in a picturebox this error message appears..:

System.IO.IOException:进程无法访问文件'C:\ Users \ ALI PC \ Documents \ My Projects \ Doctor Clinic \ DoctorClinic \ Images \ 1.jpg",因为它正在被另一个进程使用.
在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔值useRights,FileShare共享,Int32bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)
在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,位于System.IO.FileStream..ctor(String)上的布尔bFromProxy)路径,FileMode模式)位于Doctor_Clinic.Forms.ReportForm.deleteBtn_Click(对象发送方,c:\ Users \ ALI PC \ Documents \ My Projects \ Doctor中的EventArgs e)诊所\医生诊所\表格\ ReportForm.cs:第94行

System.IO.IOException: The process cannot access the file 'C:\Users\ALI PC\Documents\My Projects\Doctor Clinic\Doctor Clinic\Images\1.jpg' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at Doctor_Clinic.Forms.ReportForm.deleteBtn_Click(Object sender, EventArgs e) in c:\Users\ALI PC\Documents\My Projects\Doctor Clinic\Doctor Clinic\Forms\ReportForm.cs:line 94

var file = @image;
using (var s = new System.IO.FileStream(file, System.IO.FileMode.Open))
{
    PatientImage.Image = Image.FromStream(s);
}
PatientImage.Image = null;
PatientImage.Image.Dispose();
System.IO.File.Delete(file);

推荐答案

您的标题具有误导性.引发的行不是 Delete ,而是 Dispose .

Your title is misleading. The line that throws is not the Delete but the Dispose.

您已经将其设置为 null ,因此不能再对其进行 Dispose 了.改用它:

You have already set it to null, so you can't Dispose of it any more. Use this instead:

   ..
   if (PatientImage.Image != null)
   {
      Image dummy = PatientImage.Image; 
      PatientImage.Image = null; 
      dummy.Dispose(); 
   }
   ..

首先,我们为图像存储一个新的虚拟引用;然后从控件中清除引用,最后使用虚拟引用释放GDI资源.

First we store a new dummy reference to the image; then we clear the reference from the control and finally we use the dummy reference to free the GDI resources.

每当您要为 PictureBox 设置新的 Image 时,也建议这样做.

This is also recommended whenever you want to set a new Image to a PictureBox.

(这看起来比处理 reference 变量时要复杂的多;但这不是 PictureBox.Image 是什么.这是一个属性,幕后还有各种各样的附加功能.)

(This looks a little more complicated than one would expect when dealing with reference variables; but that is not what PictureBox.Image is. It is a property with all sorts of extras going on behind the scenes..)

这篇关于我尝试删除图片框中使用的图像时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆