[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

09/28/2005 at 08:50AM PDT, ID: 21577160
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.0

Error in saving TIFF in CCITT4 compression format

Asked by pra_kumar03 in C# Programming Language

Tags: tiff

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(iIndxTiffName+1);
                        int iTiffLength =strTiffName.Length;
                        string strPath = strImagePath.Remove(iIndxTiffName +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)EncoderValue.CompressionNone);
                        ep.Param[0] = new EncoderParameter(Encoder.Compression,(long)EncoderValue.CompressionCCITT4);                         
                        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(strImagePath);      
                        FrameDimension oDimension = new FrameDimension(pages.FrameDimensionsList[0]);
                        int FrameCount = pages.GetFrameCount(oDimension);                                                            
                        ArrayList TiffFiles = new ArrayList();                        
                        for(int frame = 0; frame< FrameCount; frame++)
                        {                                    
                              pages.SelectActiveFrame(oDimension,frame);            
                              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(TiffFiles,ref iframe,ref sOutPath,ref strMessage);            
                        //delete the Object.
                        pages.Dispose();
                        //delete the original file
                        File.Delete(strImagePath);                        
                        //copy the compressed.
                        File.Copy(sOutPath,strImagePath);
                        //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_NAME,strMessage,EventLogEntryType.Error);  
                        return 1;
                  }                  
                  return 0;
            }


      private int  AppendSplittedTiffImages(ArrayList 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)EncoderValue.MultiFrame);
                        ep.Param[1] = new EncoderParameter(Encoder.Compression ,(long)EncoderValue.CompressionLZW);

                        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,strMessage,EventLogEntryType.Information);
                        //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(IndexOfSlash +1);
                              int strImageFileLength = sImageFile.Length;
                              string Path= strImageFile.Remove(IndexOfSlash +1,strImageFileLength);
                              string sNewPath = Path.Substring(0,Path.Length -1);
                              int sDirIndx = sNewPath.LastIndexOf("\\");
                              string sDirName = sNewPath.Substring(sDirIndx + 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.FromFile(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)EncoderValue.FrameDimensionPage);
                                    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)EncoderValue.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_NAME,strMessage,EventLogEntryType.Error);  
                        Console.WriteLine(strMessage);
                        return 1;
                  }
                  
                  return 0;
            }            

            
            //Get EncoderInfo - For the Mime Type      
            private ImageCodecInfo GetEncoderInfo(ref string mimeType)
            {
                  ImageCodecInfo[] encoders=ImageCodecInfo.GetImageEncoders();
                  for (int j=0;j<encoders.Length;j++)
                  {
                        if (encoders[j].MimeType==mimeType)
                              return encoders[j];
                  }
                  
                  throw new Exception( mimeType + " mime type not found in ImageCodecInfo" );
            }


[+][-]01/02/06 10:56 PM, ID: 15596810

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: tiff
Sign Up Now!
Solution Provided By: Netminder
Participating Experts: 3
Solution Grade: B
 
 
[+][-]09/28/05 09:16 AM, ID: 14976261

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/28/05 09:32 AM, ID: 14976413

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/28/05 10:21 AM, ID: 14976969

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/28/05 11:25 AM, ID: 14977574

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/28/05 11:35 AM, ID: 14977673

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/28/05 12:00 PM, ID: 14977923

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/28/05 12:01 PM, ID: 14977933

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/17/05 12:33 PM, ID: 15102338

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/29/05 07:50 PM, ID: 15575651

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091028-EE-VQP-86