I get a 'Parameter not valid error' on the second iteration when calling SelectActivePage and I am using version 2.5.1.0. If I use 2.3.0.0 of the dll, I don't get the error.
private
void RotateAllFrames(Image pImage, string pFileName)
{
// jpoe ht 398626 05/14/2008 - rotate landscape orientation to portrait orientation
if (_TaskManager != null)
{
_TaskManager.ReportProgress(
"Rotating " + Path.GetFileName(pFileName),
String.Empty,
-1,
false);
Application.DoEvents();
}
string tempFileName = String.Empty;
//get the codec for tiff files
ImageCodecInfo info = null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
{
if (ice.MimeType == "image/tiff")
{
info = ice;
}
}
//use the save & compression encoder
System.Drawing.Imaging.
Encoder encSave = System.Drawing.Imaging.Encoder.SaveFlag;
System.Drawing.Imaging.
Encoder encComp = System.Drawing.Imaging.Encoder.Compression;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] =
new EncoderParameter(encSave, (long)EncoderValue.MultiFrame);
ep.Param[1] =
new EncoderParameter(encComp, (long)EncoderValue.CompressionCCITT4);
Bitmap pages = null;
Guid index = pImage.FrameDimensionsList[0];
System.Drawing.Imaging.
FrameDimension dimension;
dimension =
new System.Drawing.Imaging.FrameDimension(index);
//Total frame numbers
int totalFrame = pImage.GetFrameCount(dimension);
try
{
for (int frame = 0; frame <= (totalFrame - 1); frame++)
{
pImage.SelectActiveFrame(dimension, frame); //<------------------------------------
if (frame == 0)
{
tempFileName =
"~Rotating" + Path.GetFileName(pFileName);
tempFileName =
Path.Combine(Path.GetDirectoryName(pFileName), tempFileName);
// copy the selected page
Bitmap bm = new Bitmap(pImage, pImage.Width, pImage.Height);
bm.SetResolution(pImage.HorizontalResolution, pImage.VerticalResolution);
bm.RotateFlip(
RotateFlipType.Rotate90FlipNone);
// convert to a one bit per pixel image that is compatible with compression mode
pages =
Converter.ConvertToBitonal(bm);
//save the file.
pages.Save(tempFileName, info, ep);
bm.Dispose();
}
else
{
//save the intermediate frames
ep.Param[0] =
new EncoderParameter(encSave, (long)EncoderValue.FrameDimensionPage);
//copy the selected page
Bitmap bm = new Bitmap(pImage, pImage.Width, pImage.Height);
bm.SetResolution(pImage.HorizontalResolution, pImage.VerticalResolution);
bm.RotateFlip(
RotateFlipType.Rotate90FlipNone);
// convert to a one bit per pixel image that is compatible with compression mode
Bitmap bitonalbm = Converter.ConvertToBitonal(bm);
pages.SaveAdd(bitonalbm, ep);
//g.Dispose();
bm.Dispose();
bitonalbm.Dispose();
}
if (frame == totalFrame - 1)
{
//flush and close.
ep.Param[0] =
new EncoderParameter(encSave, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
}
if (pages != null)
{
pages.Dispose();
pImage.Dispose();
// closes original file
if (tempFileName.Length > 0)
{
File.Delete(pFileName);
File.Move(tempFileName, pFileName);
}
}
}
catch (Exception e)
{
mAppSupport.WriteError(e.ToString());
mAppSupport.SendSupportNotification(e.ToString(),
false);
}
}