Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
2.11/5 (2 votes)
See more:
Hi all.

I have been experimenting with manipulating tiff images programmatically. That said my final product is loaded as a list of individual images in a list like so:
CSS
List<Bitmap> images = new List<Bitmap>();

I know the images are there as I can display them in picturebox.

My problem here is trying to collate all images and save as single multipage tiff file.

so far this is the code which I have found on the Microsoft site: Encoder.SaveFlag Field[^]

CSS
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;

// Get an ImageCodecInfo object that represents the TIFF codec.
myImageCodecInfo = GetEncoderInfo("image/tiff");

// Create an Encoder object based on the GUID 
// for the SaveFlag parameter category.
myEncoder = System.Drawing.Imaging.Encoder.SaveFlag;

// Create an EncoderParameters object. 
// An EncoderParameters object has an array of EncoderParameter 
// objects. In this case, there is only one 
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);

// Save the first page.
myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;

final_Tiff.Save(destinaton, myImageCodecInfo, myEncoderParameters);

foreach (Bitmap bmp in images)
   {
   // Save rest of the pages.
   myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
   myEncoderParameters.Param[0] = myEncoderParameter;
   final_Tiff.SaveAdd(bmp, myEncoderParameters);
   }

// Close the multiple-frame file.
myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.Flush);
myEncoderParameters.Param[0] = myEncoderParameter;
final_Tiff.SaveAdd(myEncoderParameters);


The trouble occurs at:
CSS
final_Tiff.Save(destinaton, myImageCodecInfo, myEncoderParameters);

with error message: Object reference not set to an instance of an object.

I did check these values and they are not null. Any suggestions are welcome, and thanks in advance.
Posted
Updated 19-Apr-15 19:58pm
v5
Comments
Rana Waqas 20-Apr-15 2:28am    
what is final_Tiff ? You might have declared final_Tiff globally but you haven't created an instance of it while calling.
Sergey Alexandrovich Kryukov 21-Apr-15 23:02pm    
Do you want to create multi-page tiff, or you just want to combine few images side-by-side or in some other arrangement?
Both problems are not hard to solve...
—SA
Silver13 21-Apr-15 23:17pm    
I am creating a multipage tiff. However I now have an issue with resolution where it dropped to 96 dpi, but I want it as 200 dpi? Any advice?
Sergey Alexandrovich Kryukov 21-Apr-15 23:58pm    
Resolution means nothing. It is not real resolution, but a piece of metadata indicating original source resolution, a kind of default. I already answered a similar question referencing some open-source products and articles. Would you like me to find it out for you?
—SA
Silver13 22-Apr-15 0:09am    
Yes please, any information would be helpful to better understand this. Hopefully all this information will help others. But just out of curiosity is it possible to set that metadata to 200 dpi in this code? I am just curious as I already spent a bit of time researching this and no answer so far.

1 solution

First of all, look at this open-source library: https://thetifflibrary.codeplex.com[^].

You can look at the source code to see if metadata modification is available. It should be.

There is also a number of CodeProject articles: http://www.codeproject.com/search.aspx?q=multipage+tiff+&doctypeid=1[^].

First of all, look at this one: Save images into a multi-page TIFF file or add images to an existing TIFF file[^].

See also this one: A simple TIFF management class[^].

You can ask the authors your question about the resolution.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900