We are having an issue converting PDFs to TIFs using version 9.4.12.0 of Spire.PDF from within with our full framework .NET 4.8 WinForms application. Intermittently users are getting errors (still tracking down exactly the error) or the converted TIF has blank pages or portions of pages. Users are working around the issue by closing and opening the application or having a colleague work with the image they are having problems with. The same image that is failing for one user works fine for another or after a restart.
The twist to this issue is the environment in which our uses are working with this legacy desktop app. Our users log into Azure Virtual Desktops (AVDs) to use this app. That means that multiple users are essentially logging into the same OS instance at the same time and are running the app concurrently in their own slice of a shared memory pool. Testing in isolation works just fine. This library works fine from within the context of an IIS Application Pool correct? How would a multi-user-Windows scenario be any different? In other instances we have seen issues where memory from one user degrades performance for others. Could that be related here?
For what its worth, we are using a .NET 6 version of this library in a multi-threaded background process for very similar reasons with great success. That process converts PDFs to TIFs without problems and its code is identical to the WinForms process.
- Code: Select all
public static bool SaveCertImage(byte[] pdfBytes, string destination, int dpiOverride = 0)
{
using (var document = new PdfDocument(pdfBytes))
{
Image[] images = new Image[document.Pages.Count];
int dpi = (dpiOverride > 0) ? dpiOverride : (document.Pages.Count <= 5) ? 200 : 140;
for (int i = 0; i < document.Pages.Count; i++)
{
images[i] = document.SaveAsImage(i, dpi, dpi);
}
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW);
ImageCodecInfo info = ImageCodecInfo
.GetImageEncoders()
.FirstOrDefault(encoder => encoder.MimeType == "image/tiff");
if (info == null)
throw new Exception("image/tiff" + " mime type not found in ImageCodecInfo");
using (Image tiff = images[0])
{
//save the first frame
tiff.Save(destination, info, ep);
//save the intermediate frames
for (int frame = 1; frame < images.Length; frame++)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
tiff.SaveAdd(images[frame], ep);
}
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
tiff.SaveAdd(ep);
}
Array.ForEach(images, i => i.Dispose());
return File.Exists(destination);
}
}