多页TIFF分离的性能

时间:2015-08-11 15:56:24

标签: c# image tiff

我需要分隔Multipage TIFF文件。输入文件夹包含100个TIFF文件。 100个TIFF文件所用的时间是1.40分钟。每个TIFF文件包含两个页面。有没有办法提高性能,有没有办法加快这个过程?

               static void Main(string[] args)
            {
                   string input = @"D:\testSplit\input\";
                string output = @"D:\testSplit\output\out";
                DirectoryInfo dir1 = new DirectoryInfo(input);        
                FileInfo[] DispatchFiles = dir1.GetFiles();
                if (DispatchFiles.Length > 0)
                {
                    foreach (FileInfo aFile in DispatchFiles)
                    {
                        string files = input + aFile.Name;

                        if (File.Exists(files))
                        {
                            Split(files, output);
                        }

                    }
                }
            }


            public static List<string> Split(string InputFilePath, string OutputPath)
            {
                List<string> splitFileNames = new List<string>();
                try
                {
                    //Get the frame dimension list from the image of the file and 
                      Image tiffImage = Image.FromFile(InputFilePath);                            
                    //get the globally unique identifier (GUID)        
                    Guid objGuid = tiffImage.FrameDimensionsList[0];       
                    //create the frame dimension 
                    FrameDimension dimension = new FrameDimension(objGuid);
                    //Gets the total number of frames in the .tiff file 
                    int noOfPages = tiffImage.GetFrameCount(dimension);        
                    if (noOfPages == 1)
                    {
                        splitFileNames.Add(InputFilePath);
                        tiffImage.Dispose();
                        return splitFileNames;
                    }
                    string filName = Path.GetFileNameWithoutExtension(InputFilePath);
                    string fileExtention = Path.GetExtension(InputFilePath);

                    ImageCodecInfo encodeInfo = null;
                    ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                    for (int j = 0; j < imageEncoders.Length; j++)
                    {
                        if (imageEncoders[j].MimeType == "image/tiff")
                        {
                            encodeInfo = imageEncoders[j];
                            break;
                        }

                    // Save the tiff file in the output directory. 
                    if (!Directory.Exists(OutputPath))
                        Directory.CreateDirectory(OutputPath);
                    foreach (Guid guid in tiffImage.FrameDimensionsList)
                    {
                        for (int index = 0; index < noOfPages; index++)
                        {
                            FrameDimension currentFrame = new FrameDimension(guid);

                            tiffImage.SelectActiveFrame(currentFrame, index);

                            string outPath = string.Concat(OutputPath, filName, "-P", index + 1, fileExtention);

                            tiffImage.Save(outPath, encodeInfo, null);

                            splitFileNames.Add(outPath);
                        }
                    }

                    tiffImage.Dispose();            
                    return splitFileNames;

                }
                catch (Exception ex)
                {
                    return splitFileNames;
                }
            }

1 个答案:

答案 0 :(得分:2)

parallel foreach循环可以帮助您找到所需的位置。

FileInfo[] DispatchFiles = dir1.GetFiles();
Parallel.ForEach(DispatchFiles, aFile =>
{
    string files = input + aFile.Name;
    if (File.Exists(files))
    {
        Split(files, output);
    }
}

使用并行处理可能会遇到一些共享资源问题,但如果每个处理完全相互独立,则应该会提高性能。

如果您需要限制循环创建的线程数,请查看MaxDegreeOfParallelism属性。