PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Drawing with wrong brush
https://forum.pdfsharp.net/viewtopic.php?f=3&t=878
Page 1 of 1

Author:  mried [ Sun Sep 20, 2009 2:30 pm ]
Post subject:  Drawing with wrong brush

Hi all!

I've found a bug while creating some graphics with PDF-Sharp 1.30. If you execute the following code, you would expect rectangle which is filled with a linear gradient from blue to green and a black text.

Code:
      PdfDocument document = new PdfDocument();
      PdfPage page = document.AddPage();
      XGraphics gfx = XGraphics.FromPdfPage(page);

      XBrush brush = new XLinearGradientBrush(new XPoint(0, 0), new XPoint(50, 20), XColors.Blue, XColors.Green);
      gfx.DrawRectangle(brush, 10, 10, 50, 20);

      XFont font = new XFont("Microsoft Sans Serif", 9);
      XBrush brush2 = new XSolidBrush(XColors.Black);
      gfx.DrawString("DummyString", font, brush2, 10, 50);

      document.Save(@"C:\test.pdf");


But the text isn't black here - it is rendered with the same linear gradient as the rectangle.

Digging through the code, I found a way to change this behaviour: Class PdfGraphicsState, Line 252 reads

Code:
if (this.realizedFillColor.Rgb != color.Rgb)


Change it to the following and it will work:

Code:
if (this.realizedFillColor.IsEmpty || this.realizedFillColor.Rgb != color.Rgb)


The reason is: After a linear gradient is used, the realizedFillColor is set do XColor.Empty. When drawing the text (in our example), the mentioned if statement is executed. It checks if XColor.Empty.Rgb (which is stored in realizedFillColor) equals the new XColor.Rgb (which is black in our example). This is true, because XColor.Empty.Rgb == 0 and XColor.Black.Rgb == 0.

I'm not sure if the mentioned solution is the best way. Maybe this has some sideeffects I dont't see here.

Malte

Author:  mried [ Wed Dec 16, 2009 1:54 pm ]
Post subject:  Re: Drawing with wrong brush

Hi again!

The bug can still be found in version 1.31.

Regards,

Malte

Author:  SiliconMind [ Thu Feb 17, 2011 1:19 pm ]
Post subject:  Re: Drawing with wrong brush

Similar bug seems to occur when drawing XForms.
To reproduce:
1. Crate XGraphics (graphics1) from PdfPage
2. Draw pink rectangle to graphics1
3. Create XForm using constructor XForm(graphics1, width, height)
4. Create XGraphics (graphics2) using Xgraphics.FromForm(form)
5. Draw black GraphicsPath to graphics2 (XForm)
6. Draw XForm to graphics1 using graphics1.DrawImage(form, 0,0)

Result: pink circle on a pink rectangle.
Any suggestions for a fix?

The wired part is that brush color from first drawing is used for drawing XForm, but the brush transparency is not preserved.

Author:  SiliconMind [ Tue Feb 22, 2011 6:12 pm ]
Post subject:  Re: Drawing with wrong brush

SiliconMind wrote:
Similar bug seems to occur when drawing XForms.
1. Crate XGraphics (graphics1) from PdfPage
2. Draw pink rectangle to graphics1
3. Create XForm using constructor XForm(graphics1, width, height)
4. Create XGraphics (graphics2) using Xgraphics.FromForm(form)
5. Draw black GraphicsPath to graphics2 (XForm)
6. Draw XForm to graphics1 using graphics1.DrawImage(form, 0,0)


Workaround for this bug is to save and restore XGraphicsState each time a code block responsible for drawing is created. In the example above, one should do graphics1.Save() between steps 1 and 2. Then call graphics1.Restore(state) between steps 2 and 3. Ugly but works.

Author:  Thomas Hoevel [ Mon Jan 25, 2016 9:46 am ]
Post subject:  Re: Drawing with wrong brush

The workaround is not needed when using the current version of PDFsharp (1.50, released last year). Those who want to stick to version 1.3x have to use the workaround.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/