最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Converting png bytes to pdf bytes - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

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.

发布评论

评论列表(0)

  1. 暂无评论