C#: How do I convert a multi-page TIFF via MemoryStream into one long image?

两盒软妹~` 提交于 2019-11-28 11:49:46

I'm guessing that you'll have to loop over each frame in the TIFF.

Here's an excerpt from Split multi page tiff file:

private void Split(string pstrInputFilePath, string pstrOutputPath) 
    { 
        //Get the frame dimension list from the image of the file and 
        Image tiffImage = Image.FromFile(pstrInputFilePath); 
        //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); 

        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(pstrOutputPath)) 
            Directory.CreateDirectory(pstrOutputPath); 

        foreach (Guid guid in tiffImage.FrameDimensionsList) 
        { 
            for (int index = 0; index < noOfPages; index++) 
            { 
                FrameDimension currentFrame = new FrameDimension(guid); 
                tiffImage.SelectActiveFrame(currentFrame, index); 
                tiffImage.Save(string.Concat(pstrOutputPath, @"\", index, ".TIF"), encodeInfo, null); 
            } 
        } 
    } 

You should be able to adapt the logic above to append onto your JPG rather than create separate files.

In case you get the dreadful "A generic error occurred in GDI+" error (which is arguably the Rickroll of all errors) when using the SelectActiveFrame method suggested in the other answers, I strongly suggest to use the System.Windows.Media.Imaging.TiffBitmapDecoder class instead (you will need to add a Reference to the PresentationCore.dll framework library).

Here's an example code that does just that (it puts all the TIFF frames into a list of standard Bitmaps):

List<System.Drawing.Bitmap> bmpLst = new List<System.Drawing.Bitmap>();

using (var msTemp = new MemoryStream(data))
{
    TiffBitmapDecoder decoder = new TiffBitmapDecoder(msTemp, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    int totFrames = decoder.Frames.Count;

    for (int i = 0; i < totFrames; ++i)
    {
        // Create bitmap to hold the single frame
        System.Drawing.Bitmap bmpSingleFrame = BitmapFromSource(decoder.Frames[i]);
        // add the frame (as a bitmap) to the bitmap list
        bmpLst.Add(bmpSingleFrame);
    }
}

And here's the BitmapFromSource helper method:

public static Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
    Bitmap bitmap;
    using (var outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new Bitmap(outStream);
    }
    return bitmap;
}

For further info regarding this workaround, I also suggest to read this blog post I wrote.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!