Environment:
- .NET 8
- PdfSharp 6.1.1
XImage.FromStream is not able to work with Streams that are returned from
HttpClient and will always throw
InvalidOperationException with the Text "Unsupported image format".
Repro code (see attachment):
Attachment:
code.png [ 87.96 KiB | Viewed 11766 times ]
The reason for this behavior is, that the XImage class wants to check if the Stream is a PDF file and accesses the Position-Property without checking for stream.CanSeek.
Attachment:
exc.png [ 46.63 KiB | Viewed 11766 times ]
The workaround is to store the stream again in a MemoryStream or a file (see the second test).
Some personal opinion:
Using XImage.FromStream to import PDF files into a PDF library is .. suspicious at least.
However if that's really a requirement and there's no way to handle that without seeking, a good exception would be nice for developers. Something like 'Cannot import images from non-seekable streams' or the like.