none
Bug in GDI+ DrawLines() ? RRS feed

  • Question

  • DrawLines paints faulty when the the line join mode is set to LineJoin.Round in combination with special coordinates.

     

    Code to produce the error: 

            private void OnPaint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                Pen pen = new Pen(Color.FromArgb(255, 0, 0), 12);
    
                PointF[] points =
                {
                    new PointF(40, 120),
                    new PointF(140, 120),           // first interesting point
                    new PointF(90, 60),
                    
                    // select one definition for the second point:
                    new PointF(140, 120f),         // second interesting point: NOT ok
                    //new PointF(140, 120.00002f),  // second interesting point: OK
                    
                    new PointF(140, 40),
                };
    
                // Without this line the DrawLines paints correctly
                pen.LineJoin = LineJoin.Round;
    
                // Drawing the lines
                g.DrawLines(pen, points);
    
                g.Dispose();
            }
    

    When using a slight different point (marked in the code) the painting is correct.

    What's going on?
    Is this a painting bug in GDI+?

     

    Friday, October 16, 2009 9:58 AM

All replies

  • Using floating point is always tricky, it doesn't have very predictable rounding behavior when it needs to be truncated to an integer.  Adding a smidgen like you did indeed bumps it the right way.  It works fine if you use Point[] instead of PointF[].  Recommended.

    Btw: don't dispose a Graphics instance you didn't allocate.

    Hans Passant.
    Friday, October 16, 2009 12:41 PM
    Moderator
  • Thank you for your quick answer.

    Sorry, but it is not a question of floating point rounding.
    At first I'd tested it with integer values. Please test it.
    The problem occurs when the lines are drawn back exactly to previous coordinates while the style LineJoin.Round is used.

    I still think it's a bug until you convince me otherwise.

    Hans-Jürgen
    Friday, October 16, 2009 4:53 PM
  • I think it is a bug. Interestingly you get the same result if the add the lines to a GraphicsPath and draw the path, although this could just mean that both calls are ultimately translated to the same low-level APIs. I don't know how you generate the points but a workaround might be to detect when you are folding back over a line and create a separate collection of points to draw that does not include any back tracking.
    Friday, October 16, 2009 5:28 PM
  • Thank you for your quick answer.

    Sorry, but it is not a question of floating point rounding.
    At first I'd tested it with integer values. Please test it.
    The problem occurs when the lines are drawn back exactly to previous coordinates while the style LineJoin.Round is used.

    I still think it's a bug until you convince me otherwise.

    Hans-Jürgen
    In technology that is not in current development like GDI (nearly 20 years old), WYSIWYG.  You'll have to work around any idiosyncrasy or embarce the current technology, WPF.
    Friday, October 16, 2009 6:01 PM
  • I'm not talking about 20 years old technology, GDI+ is an integral part of Win 7.
    When you are working on real products it's not simple and expensive to change to 'current technology'.


    Friday, October 16, 2009 7:35 PM
  • If you need pacification, file a report on connect.microsoft.com.
    Friday, October 16, 2009 8:06 PM
  • Hello h1453,

    I have tested the situation with GraphicsPath and get the same result. I am not sure whether it is the product issue. So I also recommend you to post it to the Microsoft Connect to get a direct feedback from the product team.

    Sincerely,
    Kira Qian
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Monday, October 19, 2009 8:12 AM