我有一个c#应用程序来合并多个TIFF文件。合并文件后,我将它保存到其他位置,并删除原始的TIFF(图像文件)。但它给了一个错误"The process cannot access the file 'D:\A\Merged.tif' because it is being used by another process."
C#:删除文件时访问被拒绝
我也使用GC.Collect的()方法来释放资源...
请帮助,如何删除这些文件?
int mergeTiffPages(string filepath,string[] path)
{
string[] sa = path;
ImageCodecInfo info = null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
if (ice.MimeType == "image/tiff")
info = ice;
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
Bitmap pages = null;
int frame = 0;
foreach (string s in sa){
if (frame == 0){
pages = (Bitmap)Image.FromFile(s);
//save the first frame
pages.Save(filepath, info, ep);
}
else{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
Bitmap bm = (Bitmap)Image.FromFile(s);
pages.SaveAdd(bm, ep);
}
if (frame == sa.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
return 1;
}
你能告诉我们你是如何合并文件的吗?合并完成后您是否关闭文件? –
直到你显示一些代码之前,你会一直对我的朋友低调。 –
显示相关代码。做研究(即将你的例外信息粘贴到你喜欢的网页搜索引擎中)。请参阅例如[进程无法访问该文件,因为它正在被另一个进程使用](http://stackoverflow.com/questions/9006568/process-cannot-access-the-file-because-it-is-being-used逐另一处理)。 – CodeCaster