Dear all,
i use the following code in a web application (ASP.NET/C#) to split a tiff image:
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);
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++;
}
}
}
File.Delete(inputFilePath);
}catch(Exception x){
}
}
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