Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
i use the following code in a web application (ASP.NET/C#) to split a tiff image:

C#
public void SplitTiffImage(string inputFilePath, EncoderValue format)
    {
try{
        using (FileStream fs = new FileStream(inputFilePath, FileMode.Open,  
               FileAccess.Read) )
        {
            using (Image temp_image = Image.FromStream(fs))
            {
                Guid objGuid = temp_image.FrameDimensionsList[0];
                FrameDimension objDimension = new FrameDimension(objGuid);

                //Saves every frame as a separate file.
                Encoder enc = Encoder.Compression;
                int curFrame = 0;
                for (int i = 0; i < page_number; i++)
                {
                    temp_image.SelectActiveFrame(objDimension, curFrame);
                    EncoderParameters ep = new EncoderParameters(1);
                    ep.Param[0] = new EncoderParameter(enc, (long)format);
                    ImageCodecInfo info = GetEncoderInfo("image/tiff");

                    string fileName = string.Format("{0}_{1}.tif", "some_name",
                                                i.ToString());

                    temp_image.Save(fileName, info, ep);               

                    curFrame++;

                }// for

            }// using imgage

        }// using filestream

        File.Delete(inputFilePath);
}catch(Exception x){
/*do something*/}
    }	


i call it like this:

SplitTiffImage(ImageFullPath , EncoderValue.CompressionNone);


i tested the code several times. sometimes it works successfully, and sometimes it fails : it saves many frames and then it fails at
temp_image.Save(fileName, info, ep)

with a general error occured at GDI+. it happens at random frames, not a particular one.

What I have tried:

i am sure the user has permissions..
i tested the code on many tiff files. it succeeds for a file, and the next time it fails for the same file !
i commented out the File.Delete statement to make sure the original file is not touched, but results didn't change
Posted
Updated 11-Mar-18 21:26pm
v5
Comments
#realJSOP 7-Mar-18 8:37am    
put the code into a try/catch block.
nina4ever 7-Mar-18 9:09am    
it already was in a try catch block, and the exception is : generic error occured at GDI+ ... at Image.Save

general error occured at GDI+ , this error raise due image file references not closed properly and you are trying create or open same image.

there for close image references at end of function. by calling Garbage collector. disposing image object .
 
Share this answer
 
Comments
nina4ever 11-Mar-18 6:58am    
thanks but as i know, image object will automatically be disposed at the end of the using block. my image object is created with a using block. and my fileStream too

using(Image temp_image ...){}
to avoid GDI+ error, i used Microsoft Windows Imaging Components (WIC) instead. here is a good page with code to split and merge tiff

Speed up Tiff file processing with Microsoft Windows Imaging Components (WIC)


and here is the splitting method :

C#
public static void SPlitTiffWIC(string fileName)
{
 
    TiffBitmapEncoder newFileEncoder = null;
 
    FileInfo fi = new FileInfo(fileName);
    using (Stream documentStream = fi.OpenRead())
    {
 
        TiffBitmapDecoder originalFileDecoder = 
        new TiffBitmapDecoder(documentStream,
            BitmapCreateOptions.PreservePixelFormat,  
            BitmapCacheOption.None);
        foreach (BitmapFrame frame in originalFileDecoder.Frames)
        {
            newFileEncoder = new TiffBitmapEncoder();
            newFileEncoder.Frames.Add(frame);
            using (FileStream stream = File.Create("c:\\tiffs\\"  
                          + Guid.NewGuid().ToString() + ".tiff"))
            {
                newFileEncoder.Save(stream);
            }
 
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900