C# .NET - multi-tiff image - Asked By andre gourdine on 05-Jan-04 01:27 PM

Earn up to 50 extra points for answering this tough question.
I'm a newcomer to C#. I'm working on a project that created multi-tiff image from single tiff images. The encoder that is return has an extra value for each property. 

System.Object: {System.Drawing.Imaging.ImageCodecInfo} 
clsid: {System.Guid} 
Clsid: {System.Guid} 
codecName: "Built-in TIFF Codec" 
CodecName: "Built-in TIFF Codec" 
DllName: null 
dllName: null 
filenameExtension: "*.TIF;*.TIFF" 
FilenameExtension: "*.TIF;*.TIFF" 
flags: 65543 
Flags: 65543 
FormatDescription: "TIFF" 
formatDescription: "TIFF" 
FormatID: {System.Guid} 
formatID: {System.Guid} 
mimeType: "image/tiff" 
MimeType: "image/tiff" 
SignatureMasks: {Length=2} 
signatureMasks: {Length=2} 
SignaturePatterns: {Length=2} 
signaturePatterns: {Length=2} 
Version: 1 
version: 1 

Please review my code and let me know what I can do to eliminate the repeating value. Thank you in advance for your help. 

I'm using the below code to accomplish my goals: 

using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 

namespace FileNetWeb_C 
{ 
/// 
/// 
public class CImage 
{ 
string strRetVal; 
public string ProcessImages(string filename, int NumImages, int nStartPage) 
{ 
//return value, either error description or the multi-tiff filename 
try 
{ 
//file info variable 
string strFileToSave = filename.Substring(0, filename.Length -4); 
// Reverse a string by converting the String to a char array // and then use the Array.Reverse method to reverse it and then // convert it back to string: // char [] strArray = strFileToSave.ToCharArray(); 
strRetVal = ReverseString(strFileToSave); 

// get the name of the file to save 
strFileToSave = string.Concat(strRetVal, nStartPage, ".TIF"); 

//multi tiff image 
System.Drawing.Image imgMulti; 
imgMulti = System.Drawing.Image.FromFile(filename); 
//single tiff image to be added to multi tiff image 
System.Drawing.Image imgToAdd; 
//encoder parameters for creating the multi-tiff image 

System.Drawing.Imaging.EncoderParameters encParamsCompress; 
encParamsCompress = new System.Drawing.Imaging.EncoderParameters(1); 
System.Drawing.Imaging.EncoderParameters encParamsMulti; 
encParamsMulti = new System.Drawing.Imaging.EncoderParameters(2); 

EncoderParameters encParamsAddFrame = new EncoderParameters(1); 
EncoderParameters encParamsCloseFrame = new EncoderParameters(1); 
//encoder objects 
Encoder encCompress = new Encoder(Encoder.Compression.Guid); 
Encoder encSave = new Encoder(Encoder.SaveFlag.Guid); 
//variable for name of file to save multi-tiff image 
// single tiff image name 
string strImgFileName = filename; 

strRetVal = ReverseString(strFileToSave); 
//get the name of the file to save 
strFileToSave = string.Concat(strRetVal, nStartPage, "multi.TIF"); 

//set encoder parameter parameters 
//C# doesn't convert to byte. encParamsCompress.Param[0] = new EncoderParameter(encCompress, Convert.ToByte(EncoderValue.CompressionCCITT4)); 
encParamsMulti.Param[0] = new EncoderParameter(encCompress, Convert.ToByte(EncoderValue.CompressionCCITT4)); 
encParamsMulti.Param[1] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.MultiFrame)); 
encParamsAddFrame.Param[0] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.FrameDimensionPage)); 
encParamsCloseFrame.Param[0] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.Flush)); 

//save the multi-tiff image 
imgMulti.Save(strFileToSave, GetCodecInfo("image/tiff"), encParamsMulti); 
for (int iIndex = 2; iIndex <= NumImages; iIndex++) 
{ 

strRetVal = ReverseString(strFileToSave); 
//get the name of the file to save 
strFileToSave = string.Concat(strRetVal, iIndex.ToString(), ".TIF"); 
//get next image 
FileInfo fi = new FileInfo(strImgFileName); 
//load the image 
imgToAdd = System.Drawing.Image.FromFile(strImgFileName); 
//add the image 
imgMulti.SaveAdd(imgToAdd, encParamsAddFrame); 

} 
//close the frame 
imgMulti.SaveAdd(encParamsCloseFrame); 
imgMulti.Dispose(); 

strRetVal = strFileToSave; 

} 
catch (Exception ex) 
{ 

//log the error and return error message 

strRetVal = ex.Message; 
} 
finally 
{ 
} 

//return the filename 
return strRetVal; 

} 

private static ImageCodecInfo GetCodecInfo(String mimeType) 
{ 
int j; 
ImageCodecInfo[] encoders; 
encoders = ImageCodecInfo.GetImageEncoders(); 
for(j = 0; j < encoders.Length; ++j) 
{ 
if(encoders[j].MimeType == mimeType) 
return encoders[j]; 
} return null; 
} 

public string ReverseString(string sFile) 
{ 
char [] strArray = sFile.ToCharArray(); 
Array.Reverse(strArray); string strReverseFileName = new string(strArray); int iLengthToSubtract = strReverseFileName.IndexOf("p"); 
string sReverse = strReverseFileName.Substring(iLengthToSubtract,(strReverseFileName.Length - iLengthToSubtract)); 
strArray = sReverse.ToCharArray(); 
Array.Reverse(strArray); 
sReverse = new string(strArray); 
return sReverse; 
} 
} 
}