Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bitmaps to one multipage TIFF image in .NET 2.0

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.

like image 962
mirezus Avatar asked Dec 29 '08 19:12

mirezus


People also ask

How do I create a multipage TIFF?

Open the JPEG image and click File->Print from the application menu. Choose TIFF Image Printer 11.0 from the list of printers and then click the Print button. Enter the same filename, choose TIFF Multipaged (*. tif) and check Append to file, then click Save.

Can you have a multi page TIFF?

Yes, a TIFF file can have multiple pages. A multipage TIFF file saves multiple pages as individual image frames. However, mostly the TIFF images are saved one page per file.

Can TIFF store multiple images?

TIFF files can store an unlimited number of separate images. Each image has an IFD, or Image File Directory, that records where that image's data and metadata is stored in the file.

What is a multi page TIFF file?

A multipage TIFF file is a single TIF file which contains multiple tif images. MTIFF files are a bit like PDF in that they contain multiple pages, but the similarity ends there.


1 Answers

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:

var encoderInfo = ImageCodecInfo.GetImageEncoders().First(i => i.MimeType == "image/tiff");  EncoderParameters encoderParams = new EncoderParameters(2); encoderParams.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone); encoderParams.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame); 

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.CompressionNone); 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.

like image 146
Otávio Décio Avatar answered Sep 19 '22 14:09

Otávio Décio