We have code to convert a PDF to a TIF that works great, but we noticed that this method is returning an Aborted status code when releasing the multi-page TIFF id. The concern here is a possible memory leak, and we are using GdPicture 13.0.37.
The issue was also present on GdPicture 13.0.36. I noticed the error after refactoring our code to use a helper class I wrote to ensure we throw an exception if the status code is not OK. It's usage is:
Code: Select all
using (var gdpicture = new GdPictureImaging())
using (var command = new GdPictureImagingCommand(gdpicture)
{
try
{
command.Invoke(g => g.ReleaseGdPictureImageId(imageId);
}
catch (GdPictureException ex)
{
GdPictureStatus status = ex.Status; // Is GdPictureStatus.Aborted
}
}
Code: Select all
public static void ConvertFileToTIF(string inFile, string outFile)
{
string extension = Path.GetExtension(inFile);
if (!File.Exists(inFile))
{
throw new FileNotFoundException("The file specified to be converted does not exist.", inFile);
}
if (!extension.Equals(".pdf", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Converting a document to a TIF is only supported for a PDF document.");
}
using (var gdi = new GdPictureImaging())
using (var gdiCommand = new GdPictureImagingCommand(gdi))
using (var pdf = new GdPicturePDF())
using (var pdfCommand = new GdPicturePDFCommand(pdf))
{
pdfCommand.Invoke(p => p.LoadFromFile(inFile, false));
const int dpi = 300;
const int jpegQuality = 100;
int pageCount = pdfCommand.Invoke(p => p.GetPageCount());
int tiffId = 0;
for (int i = 1; i <= pageCount; i++)
{
pdfCommand.Invoke(p => p.SelectPage(i));
int pageId = pdfCommand.Invoke(p => p.RenderPageToGdPictureImageEx(dpi, true));
int bitDepth = gdiCommand.Invoke(g => g.GetBitDepth(pageId));
gdiCommand.Invoke(g => g.ColorDetection(pageId, true, false, true));
if (i == 1)
{
tiffId = pageId;
gdiCommand.Invoke(g => g.TiffSaveAsMultiPageFile(tiffId,
outFile,
bitDepth == 1 ? TiffCompression.TiffCompressionCCITT4
: TiffCompression.TiffCompressionJPEG,
jpegQuality));
}
else
{
gdiCommand.Invoke(g => g.TiffAddToMultiPageFile(tiffId,
pageId,
bitDepth == 1 ? TiffCompression.TiffCompressionCCITT4
: TiffCompression.TiffCompressionJPEG,
jpegQuality));
}
// Using GdPictureImagingCommand here causes an error because:
// This is returning an Aborted status for some reason, but has no impact on the
// conversion of the file. It may cause a memory leak, but will go out of scope
// so hopefully the GC will pick it up.
//
// Needs investigated or could be a GdPicture bug (v12.0.36/37).
gdi.ReleaseGdPictureImage(pageId);
}
gdiCommand.Invoke(g => g.TiffCloseMultiPageFile(tiffId));
gdiCommand.Invoke(g => g.ReleaseGdPictureImage(tiffId));
pdfCommand.Invoke(p => p.CloseDocument());
}
if (!File.Exists(outFile))
{
throw new FileNotFoundException("The conversion encountered an error and did not produce an output file.",
outFile);
}
}