保存多页tiff而不写入整个文件

时间:2013-01-15 13:59:17

标签: c# tiff multipage

我正面临一个挑战,我需要在tiff文件的最后一页上做一些工作。所以我在下面编写了示例代码并且它确实有效,它只是运行缓慢。代码最终会被执行很多,我需要加快速度。

我已经尝试过多线程的做法,但是我没有看到结果很好,因为最后所有数据必须写入同一个文件,这似乎是耗时的部分。 / p>

我希望得到一个答案,可以告诉我如何在最后一页上工作,或者我还没有考虑过的事情。

private void rotateLastPage() {
    string inputfile = "C:\\input.tif";
    string tmpfile = inputfile + ".tmp";

    Bitmap bmap = (Bitmap)Image.FromFile(inputfile);
    int max = bmap.GetFrameCount(FrameDimension.Page);

    Image[] images = new Image[max];
    int i;
    for (i = 0; i < max; i++)
    {
            bmap.SelectActiveFrame(FrameDimension.Page, i);
            images[i] = (Image)bmap.Clone();
    }
    bmap.Dispose();

    Bitmap pages = null;
    i = 0;
    foreach(Image image in images)
    {
        try
        {
            if (i == max - 1)
            {
                image.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
            EncoderParameters encoderParameters = new EncoderParameters(2);
            encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);

            Int16 c = BitConverter.ToInt16(image.PropertyItems[Array.IndexOf(image.PropertyIdList, 0x103)].Value, 0);

            if (c != 4 && c != 5)
            {
                throw new ArgumentException("Only CCIT4 and LZW compressed images are allowed.");
            }
            else if (c == 4)
            {
                encoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
            }
            else if (c == 5)
            {
                encoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
            }

            if (i == 0)
            {
                pages = (Bitmap)image;
                ImageCodecInfo encoder = getTiffEncoder();
                pages.Save(tmpfile, encoder, encoderParameters);
            }
            else
            {
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                pages.SaveAdd(image, encoderParameters);
            }

            if (i == max - 1)
            {
                encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                pages.SaveAdd(encoderParameters);
            }
            i++;
        }
        catch (Exception e)
        {
            Console.WriteLine(String.Format("Unable to rotate page {0} in file {1} due to {2}", (i + 1).ToString(), inputfile, e.Message));
            break;
        }
    }

    foreach (Image image in images) {
        image.Dispose();
    }
    pages.Dispose();

    // eventually overwrite input file with tmp file
}

private static ImageCodecInfo getTiffEncoder()
{
    ImageCodecInfo encoder = null;
    ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
    for (int i = 0; i < encoders.Length; i++)
    {
        if (encoders[i].MimeType == "image/tiff")
        {
            encoder = encoders[i];
        }
    }
    if (encoder == null)
    {
        throw new NotSupportedException("Unable to find a tiff encoder.");
    }
    return encoder;
}

2 个答案:

答案 0 :(得分:1)

您正在使用foreach-loop和for-loop。如果您要使用List,则最后一页由images.Last()访问,第一页由images.First()访问,返回相应的图像对象。

此外,您现在似乎也在为自己保存每一张图片。我不知道你有多少页,但如果这些是一堆,那可能需要很长时间。您是否尝试过全部处理它们,然后最后将它们全部保存下来?因为访问硬盘很耗时。当我处理文本文件的某些行并将其保存到新文本文件中时出现这个问题.....如果这不符合您对“不写整个文件”的限制的意思。

希望有所帮助!

答案 1 :(得分:1)

您可以使用原始文件并仅在最后一页上保存旋转,而不是处理临时文件。 (编辑:似乎不起作用)


编辑:仅克隆第一帧,添加所有其他帧并旋转最后一帧..

这样的事情:

public void rotateLastPage()
    {
        string inputfile = @"u:\\input.tiff";

        Bitmap bmap = (Bitmap)Image.FromFile(inputfile);
        int max = bmap.GetFrameCount(FrameDimension.Page);

        try
        {
            EncoderParameters encoderParameters = GetEncoderParameters(bmap, EncoderValue.MultiFrame);
            ImageCodecInfo encoder = GetTiffEncoder();
            var firstPage = (Image)bmap.Clone();
            firstPage.Save(inputfile+".tmp", encoder, encoderParameters);
            for (int i = 1; i < max - 1; i++)
            {
                bmap.SelectActiveFrame(FrameDimension.Page, i);
                encoderParameters = GetEncoderParameters(bmap, EncoderValue.FrameDimensionPage);
                firstPage.SaveAdd(bmap, encoderParameters);
            }
            bmap.SelectActiveFrame(FrameDimension.Page, max - 1);
            bmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
            encoderParameters = GetEncoderParameters(bmap, EncoderValue.FrameDimensionPage);
            firstPage.SaveAdd(bmap, encoderParameters);
            firstPage.SaveAdd(GetEncoderParameters(EncoderValue.Flush));
        }
        catch (Exception e)
        {
            Console.WriteLine(String.Format("Unable to rotate page {0} in file {1} due to {2}", max.ToString(), inputfile, e.Message));
        }
        bmap.Dispose();
    }

    private static EncoderParameters GetEncoderParameters(Image image, EncoderValue encoderValue)
    {
        EncoderParameters encoderParameters = new EncoderParameters(2);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)encoderValue);
        encoderParameters.Param[1] = GetCompressionEncoder(image);
        return encoderParameters;
    }
    private static EncoderParameters GetEncoderParameters(EncoderValue encoderValue)
    {
        EncoderParameters encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)encoderValue);            
        return encoderParameters;
    }

    private static EncoderParameter GetCompressionEncoder(Image image)
    {
        Int16 c = BitConverter.ToInt16(image.PropertyItems[Array.IndexOf(image.PropertyIdList, 0x103)].Value, 0);

        if (c == 4)
        {
            return new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
        }
        else if (c == 5)
        {
            return new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
        }
        throw new ArgumentException("Only CCIT4 and LZW compressed images are allowed.");
    }

    private static ImageCodecInfo GetTiffEncoder()
    {
        ImageCodecInfo encoder = null;
        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
        for (int i = 0; i < encoders.Length; i++)
        {
            if (encoders[i].MimeType == "image/tiff")
            {
                encoder = encoders[i];
            }
        }
        if (encoder == null)
        {
            throw new NotSupportedException("Unable to find a tiff encoder.");
        }
        return encoder;
    }