The Function is able to Compress Image in CompressionLZW compressin format. If I change the compression type to CompressionCCITT4, it gives me "An unhandled exception of type 'System.ArgumentException'
occurred in system.drawing.dll Additional information: Invalid parameter used." exception while saving the image. I am using .NET framework 1.1.
//This function will do the Image Compression.
//Input Parameter - Tif Image Path
//Output Parameter - Compressed Tif Image.
public int CompressTiffImage(string strImagePath ,ref string strOutPath,ref string strMessage)
{
try
{
//Place Holders...
int iIndxTiffName = strImagePath.LastIndexOf("
\\");
string strTiffName = strImagePath.Substring(iIn
dxTiffName
+1);
int iTiffLength =strTiffName.Length;
string strPath = strImagePath.Remove(iIndxT
iffName +1,iTiffLength);
string sOutPath = "";
int iframe = 0;
//Encoders.
Encoder enc = Encoder.Compression;
//EncodeParameters.
EncoderParameters ep=new EncoderParameters(1);
ep = new EncoderParameters(1);
//ep.Param[0]=new EncoderParameter(enc,(long
)EncoderVa
lue.Compre
ssionNone)
;
ep.Param[0] = new EncoderParameter(Encoder.C
ompression
,(long)Enc
oderValue.
Compressio
nCCITT4);
string temp="image/tiff";
ImageCodecInfo info=GetEncoderInfo(ref temp);//Set image type to tiff
//create the Temp Tiff Directory
DirectoryInfo di = Directory.CreateDirectory(
strPath + "tifs\\");
//Splitting the Images...
Image pages= (Bitmap) Image.FromFile(strImagePat
h);
FrameDimension oDimension = new FrameDimension(pages.Frame
Dimensions
List[0]);
int FrameCount = pages.GetFrameCount(oDimen
sion);
ArrayList TiffFiles = new ArrayList();
for(int frame = 0; frame< FrameCount; frame++)
{
pages.SelectActiveFrame(oD
imension,f
rame);
string sPixelF = pages.PixelFormat.ToString
();
//pages.Save(strPath + "tifs\\" + strTiffName+frame + ".tiff",info,ep);
pages.Save(strPath + "tifs\\" + strTiffName+frame + ".tiff",ImageFormat.Tiff);
TiffFiles.Add(strPath + "tifs\\" + strTiffName+frame + ".tiff");
}
int iRet = AppendSplittedTiffImages(T
iffFiles,r
ef iframe,ref sOutPath,ref strMessage);
//delete the Object.
pages.Dispose();
//delete the original file
File.Delete(strImagePath);
//copy the compressed.
File.Copy(sOutPath,strImag
ePath);
//Delete the directory.
di.Delete(true);
//return the compressed file.
strOutPath = strImagePath;
if (iRet != 0)
{
return 1;
}
}
catch (Exception ex)
{
strOutPath = null;
strMessage = ex.ToString();
Console.Write(strMessage);
EventLog.WriteEntry(API_NA
ME,strMess
age,EventL
ogEntryTyp
e.Error);
return 1;
}
return 0;
}
private int AppendSplittedTiffImages(A
rrayList imageFiles,ref int frame ,ref string strOutPath,ref string strMessage)
{
try
{
//Place Holders...
bool bDirExists = false;
string sDirPath;
string sImageFile;
//use the save encoder
Encoder enc=Encoder.SaveFlag;
EncoderParameters ep=new EncoderParameters(2);
ep.Param[0]=new EncoderParameter(enc,(long
)EncoderVa
lue.MultiF
rame);
ep.Param[1] = new EncoderParameter(Encoder.C
ompression
,(long)EncoderValue.Compre
ssionLZW);
Bitmap pages=null;
frame=0; //Set the frame count to zero
string temp="image/tiff";
ImageCodecInfo info=GetEncoderInfo(ref temp);//Set image type to tiff
//EventLog.WriteEntry(API_
NAME,strMe
ssage,Even
tLogEntryT
ype.Inform
ation);
//For each image, append the bitmap for each image to the last image saved
foreach(string strImageFile in imageFiles)
{
int IndexOfSlash = strImageFile.LastIndexOf("
\\");
sImageFile = strImageFile.Substring(Ind
exOfSlash +1);
int strImageFileLength = sImageFile.Length;
string Path= strImageFile.Remove(IndexO
fSlash +1,strImageFileLength);
string sNewPath = Path.Substring(0,Path.Leng
th -1);
int sDirIndx = sNewPath.LastIndexOf("\\")
;
string sDirName = sNewPath.Substring(sDirInd
x + 1);
sDirPath = Path+sDirName;
//check the directory exists or not
bDirExists = Directory.Exists(sDirPath)
;
if (!bDirExists)
{
DirectoryInfo di = Directory.CreateDirectory(
sDirPath);
}
if(frame==0)
{
//strOutPath = sDirPath + sImageFile;
pages=(Bitmap)Image.FromFi
le(Path +sImageFile);
//save the first frame
pages.Save(sDirPath + "\\"+ sImageFile,info,ep);
//Store the final Appended Image Path for Returning.
strOutPath = sDirPath + "\\"+ sImageFile;
}
else
{
//save the intermediate frames
ep.Param[0]=new EncoderParameter(enc,(long
)EncoderVa
lue.FrameD
imensionPa
ge);
Bitmap bm=(Bitmap)Image.FromFile(
Path + sImageFile);
pages.SaveAdd(bm,ep);
bm.Dispose();
}
if(frame==imageFiles.Count
-1)
{
//flush and close.
ep.Param[0]=new EncoderParameter(enc,(long
)EncoderVa
lue.Flush)
;
pages.SaveAdd(ep); //Save All the frames when the last image is encountered }
}
frame++;
}
//return the final Image Path.
strMessage = "< "+ frame.ToString() + " >Images Appended Successfully";
//Clean up
pages.Dispose();
ep.Dispose();
foreach(string strImageFile in imageFiles)
{
File.Delete(strImageFile);
}
}
catch (Exception ex)
{
strOutPath = null;
strMessage = ex.ToString();
EventLog.WriteEntry(API_NA
ME,strMess
age,EventL
ogEntryTyp
e.Error);
Console.WriteLine(strMessa
ge);
return 1;
}
return 0;
}
//Get EncoderInfo - For the Mime Type
private ImageCodecInfo GetEncoderInfo(ref string mimeType)
{
ImageCodecInfo[] encoders=ImageCodecInfo.Ge
tImageEnco
ders();
for (int j=0;j<encoders.Length;j++)
{
if (encoders[j].MimeType==mim
eType)
return encoders[j];
}
throw new Exception( mimeType + " mime type not found in ImageCodecInfo" );
}