【问题标题】:Trying to improve multi-page TIFF file splitting尝试改进多页 TIFF 文件拆分
【发布时间】:2013-09-05 20:01:59
【问题描述】:

我正在尝试提高将多页 TIFF 文件拆分为单个页面的速度,这些页面存储为字节数组列表。我正在研究这个 TiffSplitter 类,以尝试提高 Paginate 方法的速度。

我听说过 LibTiff.net,想知道它是否会比这个过程更快?目前,对一个 7 页的多页 TIFF 文件调用 Paginate 方法大约需要 1333 毫秒。

有谁知道将多页 TIFF 的各个页面检索为字节数组的最有效方法是什么?或者可能对如何提高我当前使用的进程的速度有任何建议?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace TiffSplitter
{
    public class TiffPaginator
    {
        private List<byte[]> paginatedData;
        public List<byte[]> Pages
        {
            get
            {
                return paginatedData;
            }
        }

        public TiffPaginator()
        {
            paginatedData = new List<byte[]>();
        }

        public void Paginate(string Filename)
        {
            using (Image img = Image.FromFile(Filename))
            {
                paginatedData.Clear();
                int frameCount = img.GetFrameCount(FrameDimension.Page);
                for (int i = 0; i < frameCount; i++)
                {
                    img.SelectActiveFrame(new FrameDimension(img.FrameDimensionsList[0]), i);
                    using (MemoryStream memstr = new MemoryStream())
                    {
                        img.Save(memstr, ImageFormat.Tiff);
                        paginatedData.Add(memstr.ToArray());
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 你试过让这个进程多线程吗?例如:而不是 for 循环使用 Parallel.ForEach。我过去也使用 atalasoft 进行 tiff 处理,也许它可以帮助你。
  • 我在 MSDN 上找到了 this example,这让我找到了我目前正在使用的 TiffBitmapEncoder 和 TiffBitmapDecoder 的解决方案。明天回去工作时,我会发布我实际使用的内容作为答案。

标签: c# tiff


【解决方案1】:

我尝试使用 LibTiff.net,但对我来说,速度很慢。拆分单个 2 页 tif 的时间以秒为单位。 最后,我决定参考 PresentationCore 并使用: (它将图像拆分为多个文件,但将输出切换为字节数组应该很简单)

Stream imageStreamSource = new FileStream("filename", FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
int pagecount = decoder.Frames.Count;
if (pagecount > 1)
{
    string fNameBase = Path.GetFileNameWithoutExtension("filename");
    string filePath = Path.GetDirectoryName("filename");
    for (int i = 0; i < pagecount; i++)
    {
        string outputName = string.Format(@"{0}\SplitImages\{1}-{2}.tif", filePath, fNameBase, i.ToString());
        FileStream stream = new FileStream(outputName, FileMode.Create, FileAccess.Write);
        TiffBitmapEncoder encoder = new TiffBitmapEncoder();
        encoder.Frames.Add(decoder.Frames[i]);
        encoder.Save(stream);
        stream.Dispose();                        
    }
    imageStreamSource.Dispose();
}

【讨论】:

  • 我有类似的代码,有时它会给出 COMException:不支持位图像素格式。你对此有什么想法吗?我在互联网上没有找到解决方案
猜你喜欢
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-28
  • 2016-12-10
  • 2011-05-18
相关资源
最近更新 更多