0

I'm using PdfSharp to save a WPF Window into a PDF. I'm getting System.OutOfMemoryException when executing the following code:-

using (MemoryStream lMemoryStream = new MemoryStream())
{
    Package package = Package.Open(lMemoryStream, FileMode.Create);
    var doc = new System.Windows.Xps.Packaging.XpsDocument(package);
    XpsDocumentWriter writer = System.Windows.Xps.Packaging.XpsDocument.CreateXpsDocumentWriter(doc);

    double dpiScale = 600.0 / 96.0;
    var renderBitmap = new RenderTargetBitmap(Convert.ToInt32(this.Width * dpiScale),
                           Convert.ToInt32(this.Height * dpiScale),
                           600.0,
                           600.0,
                           PixelFormats.Pbgra32);
    renderBitmap.Render(this);
    var visual = new DrawingVisual();
    using (var dc = visual.RenderOpen())
    {
        dc.DrawImage(renderBitmap, new Rect(0, 0, this.Width, this.Height));
    }                

    writer.Write(visual);
    doc.Close();
    package.Close();

    var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
    XpsConverter.Convert(pdfXpsDoc, _pdfFileName, 0);
}

In the above snippet, if I change the 600.0 value to 300.0 in dpiScale while creating RenderTargetBitmap, I don't get the OutOfMemoryException but the quality of the saved PDF is not good.

How to dispose the PdfSharp XpsDocument? I believe it is causing some memory leak.

KaranKakwani
  • 160
  • 12
  • 2
    You're out of memory because you're creating a gigantic image, most likely. Disposing of the pdfXpsDoc won't help because you can't dispose it until after it's been converted, and that process is what leads to the OOME. Try converting your application to x64. If the machine has more than 4gb of memory, you'll have more room to play. Otherwise, resize the width and height to something smaller than whatever it is when you're executing this code. –  Dec 19 '17 at 14:04
  • My application is 32 bit only, however RAM is not issue. OOME is occurring in a 8GB RAM machine as well. I tried using DrawRectangle instead of DrawImage with the DrawingContext but the quality of the saved PDF is not satisfactory. Please refer to my other question at https://stackoverflow.com/questions/47871928/wpf-datagrid-gridlines-not-visible-when-saved-as-pdf – KaranKakwani Dec 19 '17 at 14:16
  • 32-bit programs cannot use more than 2 GiB of memory - even on a computer with 32 GiB. Running in 64-bit mode breaks the 2 GiB barrier and may make a difference. – I liked the old Stack Overflow Dec 19 '17 at 22:56
  • As I said, My application is 32 bit and it's a requirement. Cannot simply make it to 64 bit. – KaranKakwani Dec 20 '17 at 06:01
  • @User 241.007 Can you help me with the memory leak problem? The problem with grid line visibility is not occurring with the provided snippet but if I try to save pdf more than once I get OOME. There must be some way to release the memory used by the RenderTargetBitmap. Even if it's a big image, I don't think it's anywhere near 2GB. – KaranKakwani Dec 20 '17 at 06:07
  • When the "out of memory exception" comes on the second run then you probably do not have a "memory leak", instead you have a design problem. Use smaller values than 600. Look for `IDispose` and free objects no longer needed. I never used the XPS extension of PDFsharp and I have no idea how to turn your code snippet into something I could test on my computer. And coz I think you're on the wrong track I'm not really inclined to test this on my computer either. Do you still need the XpsConverter if you already have a bitmap? PDFsharp can handle bitmaps without XpsConverter. – I liked the old Stack Overflow Dec 20 '17 at 07:39

0 Answers0