1I'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.
2
3System.Object: {System.Drawing.Imaging.ImageCodecInfo}
4clsid: {System.Guid}
5Clsid: {System.Guid}
6codecName: "Built-in TIFF Codec"
7CodecName: "Built-in TIFF Codec"
8DllName: null
9dllName: null
10filenameExtension: "*.TIF;*.TIFF"
11FilenameExtension: "*.TIF;*.TIFF"
12flags: 65543
13Flags: 65543
14FormatDescription: "TIFF"
15formatDescription: "TIFF"
16FormatID: {System.Guid}
17formatID: {System.Guid}
18mimeType: "image/tiff"
19MimeType: "image/tiff"
20SignatureMasks: {Length=2}
21signatureMasks: {Length=2}
22SignaturePatterns: {Length=2}
23signaturePatterns: {Length=2}
24Version: 1
25version: 1
26
27Please review my code and let me know what I can do to eliminate the repeating value. Thank you in advance for your help.
28
29I'm using the below code to accomplish my goals:
30
31using System;
32using System.Drawing;
33using System.Drawing.Imaging;
34using System.IO;
35
36namespace FileNetWeb_C
37{
38///
39///
40public class CImage
41{
42string strRetVal;
43public string ProcessImages(string filename, int NumImages, int nStartPage)
44{
45//return value, either error description or the multi-tiff filename
46try
47{
48//file info variable
49string strFileToSave = filename.Substring(0, filename.Length -4);
50// 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();
51strRetVal = ReverseString(strFileToSave);
52
53// get the name of the file to save
54strFileToSave = string.Concat(strRetVal, nStartPage, ".TIF");
55
56//multi tiff image
57System.Drawing.Image imgMulti;
58imgMulti = System.Drawing.Image.FromFile(filename);
59//single tiff image to be added to multi tiff image
60System.Drawing.Image imgToAdd;
61//encoder parameters for creating the multi-tiff image
62
63System.Drawing.Imaging.EncoderParameters encParamsCompress;
64encParamsCompress = new System.Drawing.Imaging.EncoderParameters(1);
65System.Drawing.Imaging.EncoderParameters encParamsMulti;
66encParamsMulti = new System.Drawing.Imaging.EncoderParameters(2);
67
68EncoderParameters encParamsAddFrame = new EncoderParameters(1);
69EncoderParameters encParamsCloseFrame = new EncoderParameters(1);
70//encoder objects
71Encoder encCompress = new Encoder(Encoder.Compression.Guid);
72Encoder encSave = new Encoder(Encoder.SaveFlag.Guid);
73//variable for name of file to save multi-tiff image
74// single tiff image name
75string strImgFileName = filename;
76
77strRetVal = ReverseString(strFileToSave);
78//get the name of the file to save
79strFileToSave = string.Concat(strRetVal, nStartPage, "multi.TIF");
80
81//set encoder parameter parameters
82//C# doesn't convert to byte. encParamsCompress.Param[0] = new EncoderParameter(encCompress, Convert.ToByte(EncoderValue.CompressionCCITT4));
83encParamsMulti.Param[0] = new EncoderParameter(encCompress, Convert.ToByte(EncoderValue.CompressionCCITT4));
84encParamsMulti.Param[1] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.MultiFrame));
85encParamsAddFrame.Param[0] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.FrameDimensionPage));
86encParamsCloseFrame.Param[0] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.Flush));
87
88//save the multi-tiff image
89imgMulti.Save(strFileToSave, GetCodecInfo("image/tiff"), encParamsMulti);
90for (int iIndex = 2; iIndex <= NumImages; iIndex++)
91{
92
93strRetVal = ReverseString(strFileToSave);
94//get the name of the file to save
95strFileToSave = string.Concat(strRetVal, iIndex.ToString(), ".TIF");
96//get next image
97FileInfo fi = new FileInfo(strImgFileName);
98//load the image
99imgToAdd = System.Drawing.Image.FromFile(strImgFileName);
100//add the image
101imgMulti.SaveAdd(imgToAdd, encParamsAddFrame);
102
103}
104//close the frame
105imgMulti.SaveAdd(encParamsCloseFrame);
106imgMulti.Dispose();
107
108strRetVal = strFileToSave;
109
110}
111catch (Exception ex)
112{
113
114//log the error and return error message
115
116strRetVal = ex.Message;
117}
118finally
119{
120}
121
122//return the filename
123return strRetVal;
124
125}
126
127private static ImageCodecInfo GetCodecInfo(String mimeType)
128{
129int j;
130ImageCodecInfo[] encoders;
131encoders = ImageCodecInfo.GetImageEncoders();
132for(j = 0; j < encoders.Length; ++j)
133{
134if(encoders[j].MimeType == mimeType)
135return encoders[j];
136} return null;
137}
138
139public string ReverseString(string sFile)
140{
141char [] strArray = sFile.ToCharArray();
142Array.Reverse(strArray); string strReverseFileName = new string(strArray); int iLengthToSubtract = strReverseFileName.IndexOf("p");
143string sReverse = strReverseFileName.Substring(iLengthToSubtract,(strReverseFileName.Length - iLengthToSubtract));
144strArray = sReverse.ToCharArray();
145Array.Reverse(strArray);
146sReverse = new string(strArray);
147return sReverse;
148}
149}
150}
2
3System.Object: {System.Drawing.Imaging.ImageCodecInfo}
4clsid: {System.Guid}
5Clsid: {System.Guid}
6codecName: "Built-in TIFF Codec"
7CodecName: "Built-in TIFF Codec"
8DllName: null
9dllName: null
10filenameExtension: "*.TIF;*.TIFF"
11FilenameExtension: "*.TIF;*.TIFF"
12flags: 65543
13Flags: 65543
14FormatDescription: "TIFF"
15formatDescription: "TIFF"
16FormatID: {System.Guid}
17formatID: {System.Guid}
18mimeType: "image/tiff"
19MimeType: "image/tiff"
20SignatureMasks: {Length=2}
21signatureMasks: {Length=2}
22SignaturePatterns: {Length=2}
23signaturePatterns: {Length=2}
24Version: 1
25version: 1
26
27Please review my code and let me know what I can do to eliminate the repeating value. Thank you in advance for your help.
28
29I'm using the below code to accomplish my goals:
30
31using System;
32using System.Drawing;
33using System.Drawing.Imaging;
34using System.IO;
35
36namespace FileNetWeb_C
37{
38///
39///
40public class CImage
41{
42string strRetVal;
43public string ProcessImages(string filename, int NumImages, int nStartPage)
44{
45//return value, either error description or the multi-tiff filename
46try
47{
48//file info variable
49string strFileToSave = filename.Substring(0, filename.Length -4);
50// 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();
51strRetVal = ReverseString(strFileToSave);
52
53// get the name of the file to save
54strFileToSave = string.Concat(strRetVal, nStartPage, ".TIF");
55
56//multi tiff image
57System.Drawing.Image imgMulti;
58imgMulti = System.Drawing.Image.FromFile(filename);
59//single tiff image to be added to multi tiff image
60System.Drawing.Image imgToAdd;
61//encoder parameters for creating the multi-tiff image
62
63System.Drawing.Imaging.EncoderParameters encParamsCompress;
64encParamsCompress = new System.Drawing.Imaging.EncoderParameters(1);
65System.Drawing.Imaging.EncoderParameters encParamsMulti;
66encParamsMulti = new System.Drawing.Imaging.EncoderParameters(2);
67
68EncoderParameters encParamsAddFrame = new EncoderParameters(1);
69EncoderParameters encParamsCloseFrame = new EncoderParameters(1);
70//encoder objects
71Encoder encCompress = new Encoder(Encoder.Compression.Guid);
72Encoder encSave = new Encoder(Encoder.SaveFlag.Guid);
73//variable for name of file to save multi-tiff image
74// single tiff image name
75string strImgFileName = filename;
76
77strRetVal = ReverseString(strFileToSave);
78//get the name of the file to save
79strFileToSave = string.Concat(strRetVal, nStartPage, "multi.TIF");
80
81//set encoder parameter parameters
82//C# doesn't convert to byte. encParamsCompress.Param[0] = new EncoderParameter(encCompress, Convert.ToByte(EncoderValue.CompressionCCITT4));
83encParamsMulti.Param[0] = new EncoderParameter(encCompress, Convert.ToByte(EncoderValue.CompressionCCITT4));
84encParamsMulti.Param[1] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.MultiFrame));
85encParamsAddFrame.Param[0] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.FrameDimensionPage));
86encParamsCloseFrame.Param[0] = new EncoderParameter(encSave, Convert.ToByte(EncoderValue.Flush));
87
88//save the multi-tiff image
89imgMulti.Save(strFileToSave, GetCodecInfo("image/tiff"), encParamsMulti);
90for (int iIndex = 2; iIndex <= NumImages; iIndex++)
91{
92
93strRetVal = ReverseString(strFileToSave);
94//get the name of the file to save
95strFileToSave = string.Concat(strRetVal, iIndex.ToString(), ".TIF");
96//get next image
97FileInfo fi = new FileInfo(strImgFileName);
98//load the image
99imgToAdd = System.Drawing.Image.FromFile(strImgFileName);
100//add the image
101imgMulti.SaveAdd(imgToAdd, encParamsAddFrame);
102
103}
104//close the frame
105imgMulti.SaveAdd(encParamsCloseFrame);
106imgMulti.Dispose();
107
108strRetVal = strFileToSave;
109
110}
111catch (Exception ex)
112{
113
114//log the error and return error message
115
116strRetVal = ex.Message;
117}
118finally
119{
120}
121
122//return the filename
123return strRetVal;
124
125}
126
127private static ImageCodecInfo GetCodecInfo(String mimeType)
128{
129int j;
130ImageCodecInfo[] encoders;
131encoders = ImageCodecInfo.GetImageEncoders();
132for(j = 0; j < encoders.Length; ++j)
133{
134if(encoders[j].MimeType == mimeType)
135return encoders[j];
136} return null;
137}
138
139public string ReverseString(string sFile)
140{
141char [] strArray = sFile.ToCharArray();
142Array.Reverse(strArray); string strReverseFileName = new string(strArray); int iLengthToSubtract = strReverseFileName.IndexOf("p");
143string sReverse = strReverseFileName.Substring(iLengthToSubtract,(strReverseFileName.Length - iLengthToSubtract));
144strArray = sReverse.ToCharArray();
145Array.Reverse(strArray);
146sReverse = new string(strArray);
147return sReverse;
148}
149}
150}