Having a problem with CompressionCCITT3

M

Mitchell Guzman

Adding a second page to the tiff file always fails. Here is the code.
The first time it creates and compresses the image, but when I tries to
"bm.SaveAdd(eps);" it fails. Thanks.

private void CompressTIFF(string outputFile)
{
try
{
Bitmap myImage =
(Bitmap)Image.FromFile(outputFile);
int ix = 0;
Type myType = myImage.GetType();
int iCount =
myImage.GetFrameCount( FrameDimension.Page );
EncoderParameters ep = new
EncoderParameters(3);
System.Drawing.Imaging.Encoder eng =
System.Drawing.Imaging.Encoder.SaveFlag;
System.Drawing.Imaging.Encoder eng1 =
System.Drawing.Imaging.Encoder.Compression;
ep.Param[0] = new
EncoderParameter(eng,(long)EncoderValue.MultiFrame);
ep.Param[1] = new
EncoderParameter(eng1,(long)EncoderValue.CompressionCCITT3);
ep.Param[2] = new
EncoderParameter(eng,(long)EncoderValue.FrameDimensionPage);
EncoderParameters eps = new
EncoderParameters(2);

if (iCount > 1)
{

File.Copy(outputFile,outputFile.Replace(OUTPUTFILE_EXT,OUTPUTFILERE_EXT)
);
return;
}
//Walk thru each page and compress and save to file
for(ix = 0; ix < iCount; ix++ )
{
//Need a start and ending image object
myImage.SelectActiveFrame( FrameDimension.Page, ix );
Bitmap img = (Bitmap) myImage.Clone();
ImageCodecInfo info = GetEncoderInfo("image/tiff");
bm=new
Bitmap(img.Width,img.Height,PixelFormat.Format1bppIndexed);
//Need to lock the bitmap and read the pixels
BitmapData bmd=bm.LockBits(new Rectangle(0, 0, bm.Width,
bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed );
//Walk thru each row and colun and set the pixels to
either white or black monchrome
for(int y=0;y<img.Height;y++)
{
for(int x=0;x<img.Width;x++)
{
if(img.GetPixel(x,y).GetBrightness()>0.5f)
this.SetIndexedPixel(x,y,bmd,true);
}
}
//write the changed data to the image control
bm.UnlockBits(bmd);
if (ix == 0)
{
eps.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.MultiFrame);
eps.Param[1] = new
EncoderParameter(System.Drawing.Imaging.Encoder.Compression,
(long)EncoderValue.CompressionCCITT3);

bm.Save(outputFile.Replace(OUTPUTFILE_EXT,OUTPUTFILERE_EXT),info,eps);
}
else
{
eps.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.FrameDimensionPage);
bm.SaveAdd(eps);
eps.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag,
(long)EncoderValue.Flush);
bm.SaveAdd(eps);
}
}
myImage.Dispose();
bm.Dispose();
GC.Collect();
}
catch(Exception e)
{
ErrorLg er = new ErrorLg();
er.LogErrorToDisk(e.Message.ToString(),"ACATSWinFax" +
":CompressTIFF()",_path);
e = null;
er = null;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top