ImageCollection Issues
Posted: Fri Dec 05, 2008 11:19 pm
I have some code that opens a multi-page TIFF (tFile = new TiffFile(e.FullPath)) and then iterates through the pages to split, based on blank page detection, into individual PDFs .
This code below (tested with the trial component):
a) won't save at col.Save(aPath, pdf). aPath is correct. No PDF is produced. Is it because it is a trial?
b) craps out the SetImage at the start of the loop (img.SetImage(pg.GetImage())) after the first save-and-clear is called, with a System.NullReferenceException. If I comment out the Clear() just after the red line, I don't get the exception. I suspect that when I col.Add(img) to col, and then later call col.Clear(), I end up disposing the img object? If so, how should I get the TIFF pages into col "by copy"?
This code below (tested with the trial component):
a) won't save at col.Save(aPath, pdf). aPath is correct. No PDF is produced. Is it because it is a trial?
b) craps out the SetImage at the start of the loop (img.SetImage(pg.GetImage())) after the first save-and-clear is called, with a System.NullReferenceException. If I comment out the Clear() just after the red line, I don't get the exception. I suspect that when I col.Add(img) to col, and then later call col.Clear(), I end up disposing the img object? If so, how should I get the TIFF pages into col "by copy"?
Code: Select all
TiffPageCollection pgs = tFile.Pages;
if (pgs.Count == 0)
{
logWriter.WriteLine("-> No pages in multi-tiff. Done.");
logWriter.Close();
if (tFile != null)
tFile.Dispose();
return;
}
VintasoftImage img = new VintasoftImage(pgs[0].GetImage());
ImageCollection col = new ImageCollection(); // will hold collection of processed TIFFs
PdfEncoder pdf = new PdfEncoder(true, 80);
int pgIdx = 0;
int pdfIdx = 1;
float blankThreshold = float.Parse(ConfigurationSettings.AppSettings["blankThreshold"]);
string aPath = "";
// begin processing
foreach (TiffPage pg in pgs)
{
try
{
img.SetImage(pg.GetImage());
}
catch (Exception err)
{
logWriter.WriteLine(err);
logWriter.Close();
if (tFile != null)
tFile.Dispose();
return;
}
if (img.IsBlank(blankThreshold))
{ // cut it up
logWriter.WriteLine("-> Blank page at position: " + pgIdx);
if (col.Count > 0)
{ // save it if there meat in the collection
aPath = e.FullPath.Replace(".", "_") + "_" + pdfIdx + ".pdf";
logWriter.WriteLine("-> Creating PDF with " + col.Count + " pages.");
col.Save(aPath, pdf);
col.Clear();
logWriter.WriteLine("-> PDF saved at: " + aPath);
}
pdfIdx++;
}
else
{ // process, clean and add
img.Deskew(BorderColor.AutoDetect, 5, 5);
//img.Despeckle(8, 25, 30, 400);
col.Add(img);
}
pgIdx++;
}