edit multi-page tiff image using C# and .NET GDI+

G

Guest

Hello,
I am trying to write a text on Multi-page TIFF image using C# and .NET
GDI+. I have written following code to do this. When I execute this code I
get "Invalid Parameter User. at System.Drawing.Image.Save" error.

public void addAnnotationStampOnImage()
{
string strStamp = Path.GetFileNameWithoutExtension(_ImageFileName);
Size dSize;
Image iMulti = Image.FromFile(_ImageFileName);
Font Fnt;
string [] FontColor;
FontStyle FntStyle;
float FontSize, LeftPos, TopPos;
string FontName, FontColors;
int intPageCount, intP;
EncoderParameters encParamsCompress, encParamsMulti, encParamsAddFrame,
encParamsCloseFrame;
Encoder encCompress, encSave;
encParamsCompress = new EncoderParameters(1);
encParamsMulti = new EncoderParameters(2);
encParamsAddFrame = new EncoderParameters(1);
encParamsCloseFrame = new EncoderParameters(1);
encCompress = new Encoder(Encoder.Compression.Guid);
encSave = new Encoder(Encoder.SaveFlag.Guid);

encParamsCompress.Param[0] = new EncoderParameter(encCompress,
(long)EncoderValue.CompressionCCITT4);
encParamsMulti.Param[0] = new EncoderParameter(encSave,
(long)EncoderValue.MultiFrame);
encParamsMulti.Param[1] = new EncoderParameter(encCompress,
(long)EncoderValue.CompressionCCITT4);
encParamsAddFrame.Param[0] = new EncoderParameter(encSave,
(long)EncoderValue.FrameDimensionPage);
encParamsCloseFrame.Param[0] = new EncoderParameter(encSave,
(long)EncoderValue.Flush);

FrameDimension oFDimensionM = new
FrameDimension(iMulti.FrameDimensionsList[0]);
intPageCount = iMulti.GetFrameCount(oFDimensionM);

dSize = new Size(iMulti.Width, iMulti.Height);

iMulti.SelectActiveFrame(oFDimensionM, 0);
Bitmap iICN = new Bitmap(iMulti, dSize);
Graphics gImage = Graphics.FromImage(iICN);
FontSize = 28F;
FontName = "Microsoft Sans Serif";
FntStyle = FontStyle.Italic;
FontColors = "00,00,00";
FontColor = FontColors.Split(",".ToCharArray());
Fnt = new Font(FontName, FontSize, FntStyle);
TopPos = 10.0F;
LeftPos = 650.0F;
gImage.DrawString(strStamp, Fnt, new
SolidBrush(Color.FromArgb(Convert.ToInt32(FontColor[0]),
Convert.ToInt32(FontColor[1]), Convert.ToInt32(FontColor[2]))), new
PointF(LeftPos, TopPos));
iICN.Save(_ImageFileName.Replace(".", "temp."),
GetEncoderInfo("image/tiff"), encParamsCompress);
for (intP = 1; intP < intPageCount; intP++)
{
iMulti.SelectActiveFrame(oFDimensionM, intP);
iICN.SaveAdd(iMulti, encParamsAddFrame);
}
iICN.SaveAdd(encParamsCloseFrame);
iICN.Dispose();
}


If I change the above function to use
iICN.Save(_ImageFileName.Replace(".", "temp."), ImageFormat.Tiff); instead of
iICN.Save(_ImageFileName.Replace(".", "temp."),
GetEncoderInfo("image/tiff"), encParamsCompress); then I get the following "
a generic error occurred in GDI+. at System.Drawing.Image.SaveAdd.

Please help.

Prasad
 
B

Bob Powell [MVP]

I answered this over in the drawing newsgroup.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Prasad More said:
Hello,
I am trying to write a text on Multi-page TIFF image using C# and
.NET
GDI+. I have written following code to do this. When I execute this code I
get "Invalid Parameter User. at System.Drawing.Image.Save" error.

public void addAnnotationStampOnImage()
{
string strStamp = Path.GetFileNameWithoutExtension(_ImageFileName);
Size dSize;
Image iMulti = Image.FromFile(_ImageFileName);
Font Fnt;
string [] FontColor;
FontStyle FntStyle;
float FontSize, LeftPos, TopPos;
string FontName, FontColors;
int intPageCount, intP;
EncoderParameters encParamsCompress, encParamsMulti, encParamsAddFrame,
encParamsCloseFrame;
Encoder encCompress, encSave;
encParamsCompress = new EncoderParameters(1);
encParamsMulti = new EncoderParameters(2);
encParamsAddFrame = new EncoderParameters(1);
encParamsCloseFrame = new EncoderParameters(1);
encCompress = new Encoder(Encoder.Compression.Guid);
encSave = new Encoder(Encoder.SaveFlag.Guid);

encParamsCompress.Param[0] = new EncoderParameter(encCompress,
(long)EncoderValue.CompressionCCITT4);
encParamsMulti.Param[0] = new EncoderParameter(encSave,
(long)EncoderValue.MultiFrame);
encParamsMulti.Param[1] = new EncoderParameter(encCompress,
(long)EncoderValue.CompressionCCITT4);
encParamsAddFrame.Param[0] = new EncoderParameter(encSave,
(long)EncoderValue.FrameDimensionPage);
encParamsCloseFrame.Param[0] = new EncoderParameter(encSave,
(long)EncoderValue.Flush);

FrameDimension oFDimensionM = new
FrameDimension(iMulti.FrameDimensionsList[0]);
intPageCount = iMulti.GetFrameCount(oFDimensionM);

dSize = new Size(iMulti.Width, iMulti.Height);

iMulti.SelectActiveFrame(oFDimensionM, 0);
Bitmap iICN = new Bitmap(iMulti, dSize);
Graphics gImage = Graphics.FromImage(iICN);
FontSize = 28F;
FontName = "Microsoft Sans Serif";
FntStyle = FontStyle.Italic;
FontColors = "00,00,00";
FontColor = FontColors.Split(",".ToCharArray());
Fnt = new Font(FontName, FontSize, FntStyle);
TopPos = 10.0F;
LeftPos = 650.0F;
gImage.DrawString(strStamp, Fnt, new
SolidBrush(Color.FromArgb(Convert.ToInt32(FontColor[0]),
Convert.ToInt32(FontColor[1]), Convert.ToInt32(FontColor[2]))), new
PointF(LeftPos, TopPos));
iICN.Save(_ImageFileName.Replace(".", "temp."),
GetEncoderInfo("image/tiff"), encParamsCompress);
for (intP = 1; intP < intPageCount; intP++)
{
iMulti.SelectActiveFrame(oFDimensionM, intP);
iICN.SaveAdd(iMulti, encParamsAddFrame);
}
iICN.SaveAdd(encParamsCloseFrame);
iICN.Dispose();
}


If I change the above function to use
iICN.Save(_ImageFileName.Replace(".", "temp."), ImageFormat.Tiff); instead
of
iICN.Save(_ImageFileName.Replace(".", "temp."),
GetEncoderInfo("image/tiff"), encParamsCompress); then I get the following
"
a generic error occurred in GDI+. at System.Drawing.Image.SaveAdd.

Please help.

Prasad
 
Top