Welcome to the WPF feedback portal. We’re happy you’re here! If you have feedback on how to improve the WPF, we’d love to hear it!>
Thanks for joining our community and helping improve Syncfusion products!
When using the PdfToImageConverter, there needs to be the option when loading a Stream called keepOpen. This flag will be used to ensure that when PdfToImageConverter is disposed, if keepOpen is true, that the underlying stream is not disposed.
This is a standard flag used in other C# classes such as StreamReader, etc.
Usage example:
public static async Task<List<MemoryStream>> ConvertToImages(Stream pdf, int w = 0, int h = 0)
{try
{using PdfToImageConverter imageConverter = new PdfToImageConverter();
imageConverter.Load(pdf, keepOpen: true);
The Dispose method of PdfToImageConverter would then be changed to:
internal void Dispose(bool disposeCompletely)
{if (Document != IntPtr.Zero)
{Stream stream = StreamManager.Get(DocumentID);
if (stream != null && !keepOpen)
{stream.Dispose();
stream = null;
}
StreamManager.Unregister(DocumentID);
if (!Environment.Is64BitProcess)
{StreamManager.Unregister((int)Document);
}
PdfiumViewer.FPDF_CloseDocument(Document);
Document = IntPtr.Zero;
if (disposeCompletely)
{StreamManager.Clear();
}
}
PageCount = 0;
}
The goal of this is you cannot always assume the caller wants you to dispose of their stream. It may be a FileStream that is opened, used by the PdfToImageConverter class, and then the user may want to do other things with that stream. But now they get hit with a stream disposed exception and there is nothing they can do. The only way to fix this is by VERY inefficiently copying the original stream to a MemoryStream and then using the MemoryStream in the PdfToImageConverter so the MemoryStream is disposed but not the original stream. Again this is very inefficient.
A keepOpen flag would resolve this and be consistent with other stream manipulation libraries in the .NET world.
Tested with version: 28.2.7