1
for (int i = 0; i < PrefixDistinctCount; i++)
                {
                    string FirstPolicy = "";

                    FirstPolicy = OriginalPics[0].ToString().Substring(26, 7);

                    var SamePoliciesQuery = OriginalPics.Where(x => Path.GetFileName(x).StartsWith(FirstPolicy));

                    foreach (string picture in SamePoliciesQuery)
                    {
                        File.Move(picture, AppVars.ProcessingPolicyImagesDirectory + picture.Substring(26, 12) + ".jpg");
                    }

                    string[] SamePolicyPics = Directory.GetFiles(AppVars.ProcessingPolicyImagesDirectory);
                    GenerateTiffFile(SamePolicyPics);

                    Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);
                    Directory.CreateDirectory(AppVars.ProcessingPolicyImagesDirectory);

                    OriginalPics = Directory.GetFiles(SelectedDirectory);
                }

到最后它说“Directory.Delete(AppVars.ProcessingPolicyImagesDirectory, true);” 我收到一个错误,它不允许我删除文件。我猜这是因为代码中的某些内容没有关闭文件或没有关闭文件。有谁知道可能是什么情况?或者我将如何使用“使用”来解决这个问题?

IOException 未处理。该进程无法访问文件“blah blah”,因为它正被另一个进程使用。

这是 GenerateTiffFile 的代码 -

public void GenerateTiffFile(string[] SamePolicyPics)
        {
            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 picture in SamePolicyPics)
            {
                if (frame == 0)
                {
                    pages = (Bitmap)Image.FromFile(picture);
                    //pages.Save(picture.Substring(0, picture.Length - 3) + "tiff", info, ep);
                    pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7)  + ".tiff", info, ep);
                }
                else
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                    Bitmap bm = (Bitmap)Image.FromFile(picture);
                    pages.SaveAdd(bm, ep);
                }

                if (frame == SamePolicyPics.Length - 1)
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
4

3 回答 3

2
Bitmap bm = (Bitmap)Image.FromFile(picture);

如手册所述,这将保持文件锁定直到bm被处理。将其包装在一个块中:using

using (Bitmap bm = (Bitmap)Image.FromFile(picture))
{
   // your code
}
于 2012-07-27T13:38:41.983 回答
1
using(Bitmap pages = (Bitmap)Image.FromFile(SamePolicyPics[0])){
        int frame = 0;

        foreach (string picture in SamePolicyPics)
        {
            if (frame == 0)
            {
                pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7)  + ".tiff", info, ep);
            }
            else
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                using(Bitmap bm = (Bitmap)Image.FromFile(picture)){
                    pages.SaveAdd(bm, ep);
                }
            }

            if (frame == SamePolicyPics.Length - 1)
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                pages.SaveAdd(ep);
            }
            frame++;
        }}
于 2012-07-27T14:20:23.463 回答
0

这将需要对您的代码进行一些重写,因为该对象将仅存在于 using 范围内。但这个想法是这样的

foreach (string picture in SamePolicyPics)
{
    using (pages = (Bitmap)Image.FromFile(picture))
    {
        if (frame == 0)
        {
            pages.Save(AppVars.FinalPolicyImagesDirectory + picture.Substring(29, 7)  + ".tiff", info, ep);
        }
        else
        {
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
            using (Bitmap bm = (Bitmap)Image.FromFile(picture))
            {
              pages.SaveAdd(bm, ep);
            }
        }

        if (frame == SamePolicyPics.Length - 1)
        {
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
            pages.SaveAdd(ep);
        }
    }
    frame++;
}
于 2012-07-27T13:55:59.503 回答