先来说一下背景,软件有一个导入jpg照片的功能,当将照片导入后,利用缩略图生成函数,生成原始照片的缩略图,之后将缩略图加载到imagelist中,在listview的largeicon属性,绑定imagelist,来显示缩略图。
问题出现在,关闭软件前,在FormClosed事件里写入了清除各种资源的代码,其中首先清除了imagelist中的image资源,清楚了listview中的items资源。这之后,在用for语句清除缩略图文件夹下的缩略图时,报出错误:“文件“C:\RoverLocatorDataBackup\thumbnail\_MG_0432.JPG”正由另一进程使用,因此该进程无法访问该文件。” 和该错误相关的代码如下:
(1) 导入像片的部分函数
//把数据加载到列表中
for (int i = 0; i < tempphotoFiles.Length; i++)
{
//将每次打开的相片地址添加到动态数组alPhotoFiles
alPhotoFiles.Add(tempphotoFiles[i]); //progressBar1运行
this.toolStripProgressBar1.PerformStep(); //调用GenThumbnail函数,生成缩略图
GenThumbnail(tempphotoFiles[i], thumbnailFilesPath + tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), 80, 64); //向photonode中添加子节点,修改选定子节点的图片,展开所有子节点
this.PhotoNode.Nodes.Add(tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), 2, 2); } //向imagelist中添加缩略图项
for (int i = 0; i < Thumbnailfileinfo.Length; i++)
{
if (photoImage != null)
{
photoImage = null;
} photoImage = Image.FromFile(Thumbnailfileinfo[i]);
this.imageList1.Images.Add(photoImage);
//向缩略图窗口中添加相片名标识
this.listView1.Items.Add(Thumbnailfileinfo[i].Substring(Thumbnailfileinfo[i].LastIndexOf(@"\") + 1), i);
} PhotoNode.ExpandAll(); //将动态数组转换添加到photoFiles
photoFiles = (string[])alPhotoFiles.ToArray(typeof(string)); this.toolStripProgressBar1.Visible = false; photoImage = null;(2) 软件关闭前,清除各种资源的函数
clearPhotoAndImage(); //清除treeview1中的子节点
this.TxtNode.Nodes.Clear();
this.PhotoNode.Nodes.Clear();
this.ImageNode.Nodes.Clear();
this.panoNode.Nodes.Clear(); //清除listView中的子节点
this.listView1.Items.Clear();
this.listView2.Items.Clear(); //清除imageList中的子节点
this.imageList1.Images.Clear();
txtFiles = null;
tempsourceTifFileName = null; photoFiles = null;
tempphotoFiles = null;
Thumbnailfileinfo = null; sourceTifFileName = null;
tempsourceTifFileName = null; panonum = 0; alTXTFiles.Clear();
alsourceTifFileName.Clear();
alPhotoFiles.Clear();
alpanofile.Clear(); Form.xColumn = null;
Form.yColumn = null;
Form.zColumn = null;
Form.jiao1Column = null;
Form.jiao2Column = null;
Form.jiao3Column = null; panoLocateForm.panoname.Clear(); panolocateresultForm.dt.Clear();
panolocateresultForm.lstName.Clear();
panolocateresultForm.lstX.Clear();
panolocateresultForm.lstY.Clear(); dataResultForm.lstName.Clear();
dataResultForm.lstX.Clear();
dataResultForm.lstY.Clear();
dataResultForm.lstZ.Clear();
dataResultForm.lst1.Clear();
dataResultForm.lst2.Clear();
dataResultForm.lst3.Clear();
System.IO.File.Delete(@"C:\RoverLocatorDataBackup\GCPPhotoData.txt"); //删除缩略图文件夹所有内容
if (Directory.Exists(thumbnailFilesPath))
{
string[] thumbfile = System.IO.Directory.GetFiles(thumbnailFilesPath); //这里为操作文件代码
for (int i = 0; i < thumbfile.Length; i++)
{
System.IO.File.Delete(thumbfile[i]); }
}问题就出在红色字体标识的部分,望各位高手指点,谢谢!
问题出现在,关闭软件前,在FormClosed事件里写入了清除各种资源的代码,其中首先清除了imagelist中的image资源,清楚了listview中的items资源。这之后,在用for语句清除缩略图文件夹下的缩略图时,报出错误:“文件“C:\RoverLocatorDataBackup\thumbnail\_MG_0432.JPG”正由另一进程使用,因此该进程无法访问该文件。” 和该错误相关的代码如下:
(1) 导入像片的部分函数
//把数据加载到列表中
for (int i = 0; i < tempphotoFiles.Length; i++)
{
//将每次打开的相片地址添加到动态数组alPhotoFiles
alPhotoFiles.Add(tempphotoFiles[i]); //progressBar1运行
this.toolStripProgressBar1.PerformStep(); //调用GenThumbnail函数,生成缩略图
GenThumbnail(tempphotoFiles[i], thumbnailFilesPath + tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), 80, 64); //向photonode中添加子节点,修改选定子节点的图片,展开所有子节点
this.PhotoNode.Nodes.Add(tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), tempphotoFiles[i].Substring(tempphotoFiles[i].LastIndexOf(@"\") + 1), 2, 2); } //向imagelist中添加缩略图项
for (int i = 0; i < Thumbnailfileinfo.Length; i++)
{
if (photoImage != null)
{
photoImage = null;
} photoImage = Image.FromFile(Thumbnailfileinfo[i]);
this.imageList1.Images.Add(photoImage);
//向缩略图窗口中添加相片名标识
this.listView1.Items.Add(Thumbnailfileinfo[i].Substring(Thumbnailfileinfo[i].LastIndexOf(@"\") + 1), i);
} PhotoNode.ExpandAll(); //将动态数组转换添加到photoFiles
photoFiles = (string[])alPhotoFiles.ToArray(typeof(string)); this.toolStripProgressBar1.Visible = false; photoImage = null;(2) 软件关闭前,清除各种资源的函数
clearPhotoAndImage(); //清除treeview1中的子节点
this.TxtNode.Nodes.Clear();
this.PhotoNode.Nodes.Clear();
this.ImageNode.Nodes.Clear();
this.panoNode.Nodes.Clear(); //清除listView中的子节点
this.listView1.Items.Clear();
this.listView2.Items.Clear(); //清除imageList中的子节点
this.imageList1.Images.Clear();
txtFiles = null;
tempsourceTifFileName = null; photoFiles = null;
tempphotoFiles = null;
Thumbnailfileinfo = null; sourceTifFileName = null;
tempsourceTifFileName = null; panonum = 0; alTXTFiles.Clear();
alsourceTifFileName.Clear();
alPhotoFiles.Clear();
alpanofile.Clear(); Form.xColumn = null;
Form.yColumn = null;
Form.zColumn = null;
Form.jiao1Column = null;
Form.jiao2Column = null;
Form.jiao3Column = null; panoLocateForm.panoname.Clear(); panolocateresultForm.dt.Clear();
panolocateresultForm.lstName.Clear();
panolocateresultForm.lstX.Clear();
panolocateresultForm.lstY.Clear(); dataResultForm.lstName.Clear();
dataResultForm.lstX.Clear();
dataResultForm.lstY.Clear();
dataResultForm.lstZ.Clear();
dataResultForm.lst1.Clear();
dataResultForm.lst2.Clear();
dataResultForm.lst3.Clear();
System.IO.File.Delete(@"C:\RoverLocatorDataBackup\GCPPhotoData.txt"); //删除缩略图文件夹所有内容
if (Directory.Exists(thumbnailFilesPath))
{
string[] thumbfile = System.IO.Directory.GetFiles(thumbnailFilesPath); //这里为操作文件代码
for (int i = 0; i < thumbfile.Length; i++)
{
System.IO.File.Delete(thumbfile[i]); }
}问题就出在红色字体标识的部分,望各位高手指点,谢谢!
解决方案 »
- 用C#发邮件的问题。。。大家来看看
- FtpWebRequest Serv-U
- winform 获取解决方案中 新建的文件夹里的文件
- C#合并excel单元格,操作合并后的单元格,先合并行,再和并列或者先合并列再合并行。
- 跟我学MVVM模式开发(2)
- 在vs2005里怎么把类图导到visio里
- C# 网络编程的问题?
- 问在C#里怎么把一个字符串作BASE64编码?绝对给分
- 招聘(深圳福田)
- 有没人玩过terrarium啊,怎么我老是加不进生物?出现Failed to load resources from resource file Please check your Setup
- byte[]如何转成image
- 有关分层设计:业务逻辑的何去何从
photoImage = Image.FromFile(Thumbnailfileinfo[i]);
this.imageList1.Images.Add(photoImage);
后面加上photoImage.Dispose();