Comments are available to members only. Sign up or Log in to view these comments.
Main Topics
Browse All TopicsThe 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'
//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
int iTiffLength =strTiffName.Length;
string strPath = strImagePath.Remove(iIndxT
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
ep.Param[0] = new EncoderParameter(Encoder.C
string temp="image/tiff";
ImageCodecInfo info=GetEncoderInfo(ref temp);//Set image type to tiff
//create the Temp Tiff Directory
DirectoryInfo di = Directory.CreateDirectory(
//Splitting the Images...
Image pages= (Bitmap) Image.FromFile(strImagePat
FrameDimension oDimension = new FrameDimension(pages.Frame
int FrameCount = pages.GetFrameCount(oDimen
ArrayList TiffFiles = new ArrayList();
for(int frame = 0; frame< FrameCount; frame++)
{
pages.SelectActiveFrame(oD
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
//delete the Object.
pages.Dispose();
//delete the original file
File.Delete(strImagePath);
//copy the compressed.
File.Copy(sOutPath,strImag
//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
return 1;
}
return 0;
}
private int AppendSplittedTiffImages(A
{
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
ep.Param[1] = new EncoderParameter(Encoder.C
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_
//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
int strImageFileLength = sImageFile.Length;
string Path= strImageFile.Remove(IndexO
string sNewPath = Path.Substring(0,Path.Leng
int sDirIndx = sNewPath.LastIndexOf("\\")
string sDirName = sNewPath.Substring(sDirInd
sDirPath = Path+sDirName;
//check the directory exists or not
bDirExists = Directory.Exists(sDirPath)
if (!bDirExists)
{
DirectoryInfo di = Directory.CreateDirectory(
}
if(frame==0)
{
//strOutPath = sDirPath + sImageFile;
pages=(Bitmap)Image.FromFi
//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
Bitmap bm=(Bitmap)Image.FromFile(
pages.SaveAdd(bm,ep);
bm.Dispose();
}
if(frame==imageFiles.Count
{
//flush and close.
ep.Param[0]=new EncoderParameter(enc,(long
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
Console.WriteLine(strMessa
return 1;
}
return 0;
}
//Get EncoderInfo - For the Mime Type
private ImageCodecInfo GetEncoderInfo(ref string mimeType)
{
ImageCodecInfo[] encoders=ImageCodecInfo.Ge
for (int j=0;j<encoders.Length;j++)
{
if (encoders[j].MimeType==mim
return encoders[j];
}
throw new Exception( mimeType + " mime type not found in ImageCodecInfo" );
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: Dan7elPosted on 2005-09-28 at 09:16:42ID: 14976261
Comments are available to members only. Sign up or Log in to view these comments.