Hi,
We’re converting many Tif and Tiff documents to PDF.
This seems to work but recently we have started seeing rather weird behavior.
Sometimes when we convert a TIF/TIFF our application shuts down and restarts itself.
We didn’t get any errors in our logs so the error wasnt caught by the Try-Catch clause.
Finally i found this error in the event log on the server:
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException: Safe handle has been closed.
Object name: ‘SafeHandle’.
at System.Threading.WaitHandle.WaitOneNoCheck(Int32 millisecondsTimeout)
at #=zRdoZiCfBNcPkOwEr7gA2XOZxDcxrLcw4wApAFDYOZR1wYhUsgQ==.#=z$Jjdj07iUeEm()
at #=zp3xAUc1oAysCLiHu5PrSrER85s3SSrPK0555WjcClVSw5DUPkw==.Clear()
at #=zwZ0deVJwndEhzNrTD$usuSevl3EusRLEEP1YO9s=.#=z0iGusJJzBMrNRx5Fkw==()
at #=zwZ0deVJwndEhzNrTD$usuSevl3EusRLEEP1YO9s=.ReleaseUnmanagedResources()
at Aspose.Imaging.DisposableObject.#=zXi2xAno=(Boolean #=zTJkAnbM=)
at Aspose.Imaging.DisposableObject.Finalize()
I tried to set up a test in the application but couldnt manage to reproduce the error from there.
Finally i managed to reproduce the behavior. We have another webapplication which i can run locally on my computer that calls our API (where the file conversion happens), which i also run locally.
By doing this i’ve managed to debug and found that the exception throws when we save the PDF:
pdf.Save(pdfStream);
Our code looks like this:
private async Task<string> ConvertTiffToPdf(FileRequestModel file)
{
try
{
using var pdf = new Aspose.Pdf.Document();
await using var pdfStream = new MemoryStream();
await using var ms = new MemoryStream(Convert.FromBase64String(file.FileBase64));
var myImage = new Bitmap(ms);
var dimension = new FrameDimension(myImage.FrameDimensionsList[0]);
var frameCount = myImage.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
var page = pdf.Pages.Add();
myImage.SelectActiveFrame(dimension, frameIdx);
var currentImage = new MemoryStream();
myImage.Save(currentImage, ImageFormat.Tiff);
var image = new Image { ImageStream = currentImage};
image = DownScaleImageToA4(myImage.Height, myImage.Width, image);
SetPageInfo(page);
image.IsBlackWhite = true;
page.Paragraphs.Add(image);
}
OptimizePdf(pdf);
pdf.Save(pdfStream);
pdfStream.Position = 0;
pdf.FreeMemory();
myImage.Dispose();
return Convert.ToBase64String(pdfStream.ToArray());
}
catch (Exception e)
{
_logger.Log(e, "Exception in");
throw new ConversionException("Konverteringen av " + file.FileName + " gick fel.");
}
}
private Image DownScaleImageToA4(int height, int width, Image img)
{
if (height > PageSize.A4.Height || width > PageSize.A4.Width)
{
if (height - PageSize.A4.Height > width - PageSize.A4.Width)
{
img.FixHeight = PageSize.A4.Height;
img.FixWidth = width * (PageSize.A4.Height / height);
}
else
{
img.FixWidth = PageSize.A4.Width;
img.FixHeight = height * (PageSize.A4.Width / width);
}
}
return img;
}
private void SetPageInfo(Page page)
{
page.PageInfo.Height = PageSize.A4.Height;
page.PageInfo.Width = PageSize.A4.Width;
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
}
private void OptimizePdf(Aspose.Pdf.Document pdf)
{
pdf.OptimizeSize = true;
pdf.Optimize();
pdf.OptimizeResources(OptimizationOptions.All());
}
I tried to upload my testfile but it looks like you don’t support uploading TIF.