PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Jun 23, 2021 3:30 pm

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Aug 12, 2015 5:20 am 

Joined: Fri Aug 07, 2015 7:12 pm
Posts: 3
XGraphics.FromGraphics(...) takes a (pre-existing) System.Drawing.Graphics as an argument.

Since XGraphics was not responsible for creating the Graphics object, it should not try to dispose of it in XGraphics.Dispose(...). But it does just that in PDFsharp-gdi 1.50.3638-beta.

I discovered this when trying to use PDFsharp to render an owner-drawn, subclassed System.Windows.Forms.Panel. If I have not enabled double-buffering for the Panel, it does work (despite the bug), but the application crashes if double-buffering is enabled.

My code looks like this:
using System.Windows.Forms;

class MyPanel : Panel {
    public MyPanel() {
        SetStyle( ControlStyles.SupportsTransparentBackColor, false );
        SetStyle( ControlStyles.AllPaintingInWmPaint, true );
        SetStyle( ControlStyles.OptimizedDoubleBuffer, true );

    protected override void OnPaint( PaintEventArgs e ) {
        var pageSizePoints = new XSize( ... );

        using ( var xg = XGraphics.FromGraphics( g, pageSizePoints ) ) {
            // Drawing code

Because XGraphics.Dispose() disposes the passed Graphics object, this code produces the following ArgumentException:
System.ArgumentException was unhandled
  Message=Parameter is not valid.
       at System.Drawing.Graphics.GetHdc()
       at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
       at System.Drawing.BufferedGraphics.Render()
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The crash is avoided if SetStyle( ControlStyles.OptimizedDoubleBuffer, true ) is commented out.

Reply with quote  
PostPosted: Wed Aug 12, 2015 9:15 am 
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3048
Location: Cologne, Germany
Yes, it's a bug.
We'll fix it with the next release.

Thomas Hoevel
PDFsharp Team

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group