删除图片文件出错 你的图片文件正在被Bitmap对象使用,你怎么可能删掉呢?先将Bitmap.Dispose掉后,再删就可以了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的bitmap是在ShowPicture()中定义的,pictureBox1_DoubleClick()根本没有用呀 程序全部代码如下:using System;using System.Drawing;using System.Drawing.Imaging;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.OleDb;using System.IO;namespace eBook{ public class FormMain : System.Windows.Forms.Form { private System.Windows.Forms.Splitter splitter1; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.PictureBox pictureBox1; private DataSet dataSet=new DataSet(); private System.Windows.Forms.TreeView treeView; private System.ComponentModel.IContainer components; //保存当前richTextBox内容所对应的节点树ID和节点的完整路径 private string richTextBoxId="",richTextBoxIDFullPath=""; //保存程序运行的完整路径 private string programStartPath=""; private System.Windows.Forms.ImageList imageList1; public FormMain() { InitializeComponent(); programStartPath=this.FullFilePath(System.Environment.CurrentDirectory.Trim()); } protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain)); this.treeView = new System.Windows.Forms.TreeView(); this.splitter1 = new System.Windows.Forms.Splitter(); this.panel1 = new System.Windows.Forms.Panel(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.imageList1 = new System.Windows.Forms.ImageList(this.components); this.panel1.SuspendLayout(); this.SuspendLayout(); this.treeView.AllowDrop = true; this.treeView.Dock = System.Windows.Forms.DockStyle.Left; this.treeView.ImageIndex = -1; this.treeView.Name = "treeView"; this.treeView.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {new System.Windows.Forms.TreeNode("节点1"),new System.Windows.Forms.TreeNode("节点2"),new System.Windows.Forms.TreeNode("节点3"),new System.Windows.Forms.TreeNode("节点4"),new System.Windows.Forms.TreeNode("节点5")}); this.treeView.SelectedImageIndex = -1; this.treeView.Size = new System.Drawing.Size(160, 441); this.treeView.TabIndex = 0; this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect); this.splitter1.Location = new System.Drawing.Point(160, 0); this.splitter1.Name = "splitter1"; this.splitter1.Size = new System.Drawing.Size(3, 441); this.splitter1.TabIndex = 1; this.splitter1.TabStop = false; this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {this.pictureBox1}); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(163, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(533, 441); this.panel1.TabIndex = 2; this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(529, 437); this.pictureBox1.TabIndex = 4; this.pictureBox1.TabStop = false; this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick); this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imageList1.ImageSize = new System.Drawing.Size(16, 16); this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); this.imageList1.TransparentColor = System.Drawing.Color.Transparent; this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(696, 441); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.panel1,this.splitter1,this.treeView}); this.Name = "FormMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "学习资料管理"; this.Load += new System.EventHandler(this.FormMain_Load); this.panel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new FormMain()); } private string FullFilePath(string stringPath) { string tmp=stringPath.Substring(stringPath.Length-1,1); if (tmp=="\\") return(stringPath); else return(stringPath+"\\"); } private void treeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { this.richTextBoxIDFullPath=e.Node.FullPath; this.richTextBoxId=e.Node.Tag.ToString().Trim(); ShowPicture(this.pictureBox1,programStartPath+"Image\\"+e.Node.Tag.ToString().Trim()+".jpeg"); } private void pictureBox1_DoubleClick(object sender, System.EventArgs e) { try { if (treeView.SelectedNode.Nodes.Count>0) return; OpenFileDialog openFileDialog=new OpenFileDialog(); openFileDialog.Filter="图片文件(*.BMP;*.GIF;*.JPG)|*.BMP;*.GIF;*.JPG"; if (openFileDialog.ShowDialog()==DialogResult.OK) { ShowPicture(this.pictureBox1,openFileDialog.FileName); if (this.pictureBox1.Image!=null) { try { Bitmap bitmap=new Bitmap(this.pictureBox1.Image); string pictureFileName=this.programStartPath+"Image\\"+this.richTextBoxId+".Jpeg"; bitmap.Save(pictureFileName,ImageFormat.Jpeg); bitmap.Dispose(); } catch(Exception pe) { MessageBox.Show(this,"节点:\n\n"+this.richTextBoxIDFullPath+"\n\n"+pe.Message,"Save picture Error",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } } else if (this.pictureBox1.Image!=null&&MessageBox.Show(this,"图片控件中原来有图片,是否取消原有图片?","Information",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)==DialogResult.OK) { this.pictureBox1.Image=null; string pictureFileName=this.programStartPath+"Image\\"+this.richTextBoxId+".Jpeg"; if (File.Exists(pictureFileName)) File.Delete(pictureFileName); } } catch(Exception le) { MessageBox.Show(this,le.Message,"Load Image Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void ShowPicture(PictureBox picturebox,string pictureFile) { try { if (this.treeView.SelectedNode.Nodes.Count>0) return; if (!File.Exists(pictureFile)) { picturebox.Image=null; return; } Bitmap bitmap; try { bitmap=new Bitmap(pictureFile,true); } catch(Exception pe) { MessageBox.Show(this,pe.Message,"Image File Error",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } picturebox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; picturebox.Image=bitmap; } catch(Exception se) { MessageBox.Show(this,se.Message,"Show Image Error",MessageBoxButtons.OK,MessageBoxIcon.Information); } } private void FormMain_Load(object sender, System.EventArgs e) { int num=0; foreach(TreeNode treeNode in this.treeView.Nodes) { num++; treeNode.Tag=num; } } }}但是在我删除图片时出错:File.Delete(pictureFileName);错误信息:该进程无法访问文件"图片文件的完整路径和文件名(该文件是存在的)",因为该文件正由另一进程使用。请教为什么会出错,我要如何才能将这个文件删除? 你的代码还有其它部分,我无法编译调试,如果能确定是你的程序导致的锁定,我建议:你在程序启动前,删除掉目标文件然后再启动启动程序,按正常方式操作,并执行 File.Delete(pictureFileName); 所在的子程序如果在这之前有锁定式操作,那么肯定会先File.Delete 而出错,因为文件不存在,这样你就知道到底是哪个先锁定式操作了 to:ArLi2003(阿利 无业悠民) 以上就是程序全部代码,而且我也用这个代码编译运行过,那应该是程序运行路径下少一个Image目录吧。我又发现了一个新的问题,就是当前节点对应的pictureBox中有图片时,再重新加载一个新的图片时,会出错:"GDI+中发生和般性错误" System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));这一句只要存在,就说明至少差一个 formmain.resx 文件所以在我这不能编译通过。另外,你的重新加载是指?如果是直接对它的image 属性付值是不会出错的呀。你能将你的代码发给我 [email protected] 我帮你瞧瞧吧 ArLi2003(阿利 无业悠民) 大侠,已经发到你的邮箱中了。麻烦你了,小兄在些谢过了 ArLi2003(阿利 无业悠民) ( ) 真好。;希望我有问题的时候也能这么好就好了。。呵呵。。假如需要我帮忙的话。也把你的代码发给我吧。[email protected] 问题找到了:treeView_AfterSelect 中已经执行过了:ShowPicture(this.pictureBox1,programStartPath+"Image\\"+e.Node.Tag.ToString().Trim()+".jpeg"); 所以已经执行了ShowPicture中的bitmap=new Bitmap(pictureFile,true);由于文件受 bitmap 引用(因为它是picbox的image 付值源,所以不能Dispose ),所以你在pictureBox1_DoubleClick 中 save 它是不受写的,所以就出错了。。你应该使用第三方源委托,解决方法为:将你原来ShowPicture 中的:try{ bitmap=new Bitmap(pictureFile,true);}换成:try{ FileStream fs = new FileStream(pictureFile,FileMode.Open, FileAccess.Read); int byteLength = (int)fs.Length; byte[] wf = new byte[byteLength]; fs.Read(wf,0,byteLength); fs.Close(); fs = null; bitmap=new Bitmap(new MemoryStream(wf),true);} 我自己也解决了,不过没有用FileStream的方法好,在出现错误无法解决时,有想过用这个,但不会用。谢谢ArLi2003(阿利 无业悠民) 和 yuwen16(rr) 的热心帮助,感激不尽。可以加我吗:156138382 其实 aspcn(飞刀) 上面已经提醒过了你却说没有用,没用哪可能程序一启动就能显示图?那是哪来的。。呵呵,错的是你哟。。 AppDomain创建 C#如何生成swf文件的缩略图? 列的拖动顺序 求助:请问这些代码实现的是什么功能? 请问那些acm出身的前辈 求GridView或者AspxGridView控件的数据筛选问题 求救!有关求平均的方法。 codesmith如何连接oracle的数据库??? 请问如何在某个控件的某个事件中触发另外一个控件的某个事件? C#不跨出WINDOWS平台,就没有可能与JAVA相比。 请问C#当中如何使用ADO( 而不是ADO.NET)? 文本编辑问题
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;
namespace eBook
{
public class FormMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pictureBox1;
private DataSet dataSet=new DataSet();
private System.Windows.Forms.TreeView treeView;
private System.ComponentModel.IContainer components;
//保存当前richTextBox内容所对应的节点树ID和节点的完整路径
private string richTextBoxId="",richTextBoxIDFullPath="";
//保存程序运行的完整路径
private string programStartPath="";
private System.Windows.Forms.ImageList imageList1;
public FormMain()
{
InitializeComponent();
programStartPath=this.FullFilePath(System.Environment.CurrentDirectory.Trim());
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));
this.treeView = new System.Windows.Forms.TreeView();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.panel1.SuspendLayout();
this.SuspendLayout();
this.treeView.AllowDrop = true;
this.treeView.Dock = System.Windows.Forms.DockStyle.Left;
this.treeView.ImageIndex = -1;
this.treeView.Name = "treeView";
this.treeView.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {new System.Windows.Forms.TreeNode("节点1"),new System.Windows.Forms.TreeNode("节点2"),new System.Windows.Forms.TreeNode("节点3"),new System.Windows.Forms.TreeNode("节点4"),new System.Windows.Forms.TreeNode("节点5")});
this.treeView.SelectedImageIndex = -1;
this.treeView.Size = new System.Drawing.Size(160, 441);
this.treeView.TabIndex = 0;
this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
this.splitter1.Location = new System.Drawing.Point(160, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 441);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {this.pictureBox1});
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(163, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(533, 441);
this.panel1.TabIndex = 2;
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(529, 437);
this.pictureBox1.TabIndex = 4;
this.pictureBox1.TabStop = false;
this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(696, 441);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.panel1,this.splitter1,this.treeView});
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "学习资料管理";
this.Load += new System.EventHandler(this.FormMain_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new FormMain());
}
private string FullFilePath(string stringPath)
{
string tmp=stringPath.Substring(stringPath.Length-1,1);
if (tmp=="\\")
return(stringPath);
else
return(stringPath+"\\");
}
private void treeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
this.richTextBoxIDFullPath=e.Node.FullPath;
this.richTextBoxId=e.Node.Tag.ToString().Trim();
ShowPicture(this.pictureBox1,programStartPath+"Image\\"+e.Node.Tag.ToString().Trim()+".jpeg");
}
private void pictureBox1_DoubleClick(object sender, System.EventArgs e)
{
try
{
if (treeView.SelectedNode.Nodes.Count>0)
return;
OpenFileDialog openFileDialog=new OpenFileDialog();
openFileDialog.Filter="图片文件(*.BMP;*.GIF;*.JPG)|*.BMP;*.GIF;*.JPG";
if (openFileDialog.ShowDialog()==DialogResult.OK)
{
ShowPicture(this.pictureBox1,openFileDialog.FileName);
if (this.pictureBox1.Image!=null)
{
try
{
Bitmap bitmap=new Bitmap(this.pictureBox1.Image);
string pictureFileName=this.programStartPath+"Image\\"+this.richTextBoxId+".Jpeg";
bitmap.Save(pictureFileName,ImageFormat.Jpeg);
bitmap.Dispose();
}
catch(Exception pe)
{
MessageBox.Show(this,"节点:\n\n"+this.richTextBoxIDFullPath+"\n\n"+pe.Message,"Save picture Error",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
}
else
if (this.pictureBox1.Image!=null&&MessageBox.Show(this,"图片控件中原来有图片,是否取消原有图片?","Information",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)==DialogResult.OK)
{
this.pictureBox1.Image=null;
string pictureFileName=this.programStartPath+"Image\\"+this.richTextBoxId+".Jpeg";
if (File.Exists(pictureFileName)) File.Delete(pictureFileName);
}
}
catch(Exception le)
{
MessageBox.Show(this,le.Message,"Load Image Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void ShowPicture(PictureBox picturebox,string pictureFile)
{
try
{
if (this.treeView.SelectedNode.Nodes.Count>0) return;
if (!File.Exists(pictureFile))
{
picturebox.Image=null;
return;
}
Bitmap bitmap;
try
{
bitmap=new Bitmap(pictureFile,true);
}
catch(Exception pe)
{
MessageBox.Show(this,pe.Message,"Image File Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
picturebox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
picturebox.Image=bitmap;
}
catch(Exception se)
{
MessageBox.Show(this,se.Message,"Show Image Error",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
private void FormMain_Load(object sender, System.EventArgs e)
{
int num=0;
foreach(TreeNode treeNode in this.treeView.Nodes)
{
num++;
treeNode.Tag=num;
}
}
}
}
但是在我删除图片时出错:
File.Delete(pictureFileName);
错误信息:
该进程无法访问文件"图片文件的完整路径和文件名(该文件是存在的)",因为该文件正由另一进程使用。请教为什么会出错,我要如何才能将这个文件删除?
然后再启动启动程序,按正常方式操作,并执行 File.Delete(pictureFileName); 所在的子程序如果在这之前有锁定式操作,那么肯定会先File.Delete 而出错,因为文件不存在,这样你就知道到底是哪个先锁定式操作了
以上就是程序全部代码,而且我也用这个代码编译运行过,那应该是程序运行路径下少一个Image目录吧。我又发现了一个新的问题,就是当前节点对应的pictureBox中有图片时,再重新加载一个新的图片时,会出错:
"GDI+中发生和般性错误"
所以在我这不能编译通过。另外,你的重新加载是指?如果是直接对它的image 属性付值是不会出错的呀。你能将你的代码发给我 [email protected] 我帮你瞧瞧吧
麻烦你了,小兄在些谢过了
希望我有问题的时候也能这么好就好了。。
呵呵。。
假如需要我帮忙的话。也把你的代码发给我吧。
[email protected]
ShowPicture(this.pictureBox1,programStartPath+"Image\\"+e.Node.Tag.ToString().Trim()+".jpeg"); 所以已经执行了ShowPicture中的
bitmap=new Bitmap(pictureFile,true);由于文件受 bitmap 引用(因为它是picbox的image 付值源,所以不能Dispose ),所以你在pictureBox1_DoubleClick 中 save 它是不受写的,所以就出错了。。你应该使用第三方源委托,解决方法为:将你原来ShowPicture 中的:try
{
bitmap=new Bitmap(pictureFile,true);
}换成:try{
FileStream fs = new FileStream(pictureFile,FileMode.Open, FileAccess.Read);
int byteLength = (int)fs.Length;
byte[] wf = new byte[byteLength];
fs.Read(wf,0,byteLength);
fs.Close();
fs = null;
bitmap=new Bitmap(new MemoryStream(wf),true);
}
可以加我吗:156138382