I'm trying to write a method in my Azure Function using PdfSharp that will download a png from a url, convert it to a pdf and upload it to the blob store.
However, I keep running into an issue with the MemoryStreams internal buffer. Could anyone please help explain this to me and why it is causing the error and how I might go about fixing it? Please see my code below for the ConvertPngBytesToPdfBytes
private static byte[] ConvertPngBytesToPdfBytes(byte[] pngBytes)
{
using (var imageStream = new MemoryStream(pngBytes))
{
var image = XImage.FromStream(imageStream);
using var pdfStream = new MemoryStream();
var document = new PdfDocument();
var page = document.AddPage();
page.Width = XUnit.FromPoint(image.PixelWidth * 72 / image.HorizontalResolution);
page.Height = XUnit.FromPoint(image.PixelHeight * 72 / image.VerticalResolution);
using (var gfx = XGraphics.FromPdfPage(page))
{
gfx.DrawImage(image, 0, 0, page.Width.Point, page.Height.Point);
}
document.Save(pdfStream, false);
return pdfStream.ToArray();
}
}
Its blowing up on var image = XImage.FromStream(imageStream);
and the error message is
MemoryStream's internal buffer cannot be accessed.
at System.IO.MemoryStream.GetBuffer()
at PdfSharp.Drawing.StreamReaderHelper..ctor(Stream stream, Int32 streamLength)
at PdfSharp.Drawing.Internal.ImageImporter.ImportImage(Stream stream)
at PdfSharp.Drawing.XImage.FromStream(Stream stream)```
I'm trying to write a method in my Azure Function using PdfSharp that will download a png from a url, convert it to a pdf and upload it to the blob store.
However, I keep running into an issue with the MemoryStreams internal buffer. Could anyone please help explain this to me and why it is causing the error and how I might go about fixing it? Please see my code below for the ConvertPngBytesToPdfBytes
private static byte[] ConvertPngBytesToPdfBytes(byte[] pngBytes)
{
using (var imageStream = new MemoryStream(pngBytes))
{
var image = XImage.FromStream(imageStream);
using var pdfStream = new MemoryStream();
var document = new PdfDocument();
var page = document.AddPage();
page.Width = XUnit.FromPoint(image.PixelWidth * 72 / image.HorizontalResolution);
page.Height = XUnit.FromPoint(image.PixelHeight * 72 / image.VerticalResolution);
using (var gfx = XGraphics.FromPdfPage(page))
{
gfx.DrawImage(image, 0, 0, page.Width.Point, page.Height.Point);
}
document.Save(pdfStream, false);
return pdfStream.ToArray();
}
}
Its blowing up on var image = XImage.FromStream(imageStream);
and the error message is
MemoryStream's internal buffer cannot be accessed.
at System.IO.MemoryStream.GetBuffer()
at PdfSharp.Drawing.StreamReaderHelper..ctor(Stream stream, Int32 streamLength)
at PdfSharp.Drawing.Internal.ImageImporter.ImportImage(Stream stream)
at PdfSharp.Drawing.XImage.FromStream(Stream stream)```
Share
Improve this question
asked Mar 19 at 10:16
f_oliveref_olivere
912 gold badges4 silver badges18 bronze badges
2
- as a side note: it looks like this might be fixed in source, assuming this is the same thing, although the fix there would be slower than the fix below - using the internal buffer is more efficient – Marc Gravell Commented Mar 19 at 10:38
- @MarcGravell This is documented on the PDFsharp site: docs.pdfsharp/PDFsharp/Topics/Bitmap-Images/… – I liked the old Stack Overflow Commented Mar 19 at 14:54
1 Answer
Reset to default 1- new MemoryStream(pngBytes)
+ new MemoryStream(pngBytes, 0, pngBytes.Length, false, true)
The important bit is the last true
, which makes the GetBuffer()
API accessible.