views:

3392

answers:

2

How can i convert an array of bitmaps into a brand new image of TIFF format, adding all the bitmaps as frames in this new tiff image?

using .NET 2.0.

+7  A: 

Start with the first bitmap by putting it into an Image object

Bitmap bitmap = (Bitmap)Image.FromFile(file);

Save the bitmap to memory as tiff

MemoryStream byteStream = new MemoryStream();
bitmap.Save(byteStream, ImageFormat.Tiff);

Put Tiff into another Image object

Image tiff = Image.FromStream(byteStream)

Prepare encoders:

        ImageCodecInfo encoderInfo = GetEncoderInfo("image/tiff");

        EncoderParameters encoderParams = new EncoderParameters(2);
        EncoderParameter parameter = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
        encoderParams.Param[0] = parameter;
        parameter = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
        encoderParams.Param[1] = parameter;

Save to file:

 tiff.Save(sOutFilePath, encoderInfo, encoderParams);

For subsequent pages, prepare encoders:

    EncoderParameters EncoderParams = new EncoderParameters(2);
    EncoderParameter SaveEncodeParam = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
    EncoderParameter CompressionEncodeParam = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
    EncoderParams.Param[0] = CompressionEncodeParam;
    EncoderParams.Param[1] = SaveEncodeParam;
    tiff.SaveAdd(/* next image as tiff - do the same as above with memory */, EncoderParams);
    }

Finally flush the file:

    EncoderParameter SaveEncodeParam = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
    EncoderParams = new EncoderParameters(1);
    EncoderParams.Param[0] = SaveEncodeParam;
    tiff.SaveAdd(EncoderParams);

That should get you started.

Otávio Décio
Excellent answer. Could only be more complete if you describe how you arrived at it (where you learned it if not from trial and error) since the MSDN docs make it next to impossible to understand.
Peter LaComb Jr.
Here's a good place to start: http://www.bobpowell.net/generating_multipage_tiffs.htm and yes, there was quite a bit of trial and error.
Otávio Décio
A: 

Not being a fan of Microsoft's track record when it comes to handling and creating files of standardized formats, I would suggest using ImageMagick, available as a .Net library in the form of MagickNet (beware, http://midimick.com/magicknet/ currently has a spyware popup, I have alerted the site owner).

Sparr