none
Lines was broken when drawing with drawlines. RRS feed

  • Question

  • lines was broken when i draw them with the following code. if changing the drawlines with drawline, the lines is ok.

    drawcurve can also draw continues lines. but the graphic is different from drawline which was like to be smoothed.

    what is the difference between drawlines and drawline?     i want more information.

    there is a discussion like this question.

    http://stackoverflow.com/questions/24105757/c-sharp-graphics-drawlines-draws-broken-lines

    it mentioned "short line". 

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.ClientSize = new Size(233, 233);
            }

            protected override void OnPaint(PaintEventArgs e)
            {
                Pen framePen = new Pen(Color.Black);
                framePen.Width = 1.5f;
                PointF[] points = new PointF[3];
                points[0] = new PointF(13.6470585f, 38.6667f);
                points[1] = new PointF(14.5568628f, 228.397522f);
                points[2] = new PointF(15.4666662f, 232.0f);
                //points[1] = new PointF(14.5568628f, 228.397522f);
                //framePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
                //framePen.MiterLimit = framePen.Width * 20f;
                //framePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Miter;

               // p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
                //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                //e.Graphics.DrawLine(framePen, points[0], points[1]);
                //e.Graphics.DrawLine(framePen, points[1], points[2]);
                e.Graphics.DrawLines(framePen, points);
                base.OnPaint(e);
            }
        }


    • Edited by Andrew, Yuan Tuesday, September 2, 2014 2:57 AM
    • Moved by CoolDadTx Wednesday, September 3, 2014 2:00 PM Winforms related
    Tuesday, September 2, 2014 2:55 AM

All replies

  • Hi Andrew,

    According to your description, you would like  to know that why the lines drawed by the Drawline and Drawlines are look like broken.

    Actually the lines drawed by the DrawLine or Drawlines are bitmaps. Bitmap constituted by a single point which is called pixel. These pixels can be arranged differently and dyed to form a pattern. When you zoom in the bitmap, you can see the countless individual squares. The effect of expanding bitmap is the increase of the size of a single pixel, so that the lines and shapes seem mixed. However, if you watch it from a distant location, the bitmap looks like continuous.

    By the way, You mentioned "the short line" before, it may be more likely to be bitmaps. And the difference between Drawline and Drawlines is that the DrawLines can Draw a series of line segments that connect an array of Point structures and the DrawLine Draws a line connecting two Point structures.

    If you have any other concern regarding this issue, please feel free to let me know.

    Best regards,

    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, September 3, 2014 12:28 PM
    Moderator
  • Maybe i did not describe the problem clearly.

    if you have visual studio ide you can excute the code. then you will see what the problem is.

    when i use GDI+ in c# to draw continuous lines with the above code.

    i draw each line seprately as follow,the lines is ok.

    e.Graphics.DrawLine(framePen, points[0], points[1]);

    e.Graphics.DrawLine(framePen, points[1], points[2]);

    but when i draw lines as follow, the lines are broken.

    e.Graphics.DrawLines(framePen, points);

    i did not try to zoom in or zoom out the lines.


    Friday, September 5, 2014 3:35 AM
  • Hi Andrew, Yuan

    I've tested your code, here is my code for testing.

    protected override void OnPaint(PaintEventArgs e)
            {
                Pen framePen = new Pen(Color.Black);
                framePen.Width = 3;
                PointF[] points = new PointF[3];
                points[0] = new PointF(13.6470585f, 38.6667f);
                points[1] = new PointF(14.5568628f, 228.397522f);
                points[2] = new PointF(215.4666662f, 232.0f);
                PointF pf1 = new PointF(53.6470585f, 78.6667f);
                PointF pf2 = new PointF(54.5568628f, 268.397522f);
                PointF pf3 = new PointF(255.4666662f, 272.0f);
                //points[1] = new PointF(14.5568628f, 228.397522f);
                //framePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
                //framePen.MiterLimit = framePen.Width * 20f;
                //framePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Miter;
                e.Graphics.SmoothingMode = SmoothingMode.None;
                // p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
                //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                //e.Graphics.DrawLine(framePen, points[0], points[1]);
                //e.Graphics.DrawLine(framePen, points[1], points[2]);
                e.Graphics.DrawLine(framePen, pf1, pf2);
                e.Graphics.DrawLine(framePen, pf2, pf3);
                e.Graphics.DrawLines(framePen,points);
                base.OnPaint(e);
            

    I draw two lines to one form, one is generated by DrawLine, the other is generated by DrawLines, but I found the two lines is the same.

    Here is my result.

    If you could give me the sample, I could help you better.

    Best regards,

    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 5, 2014 9:15 AM
    Moderator
  • Hi Andrew,

    By researching and testing again, we find this is a known issue and it is a by-design issue.

    To work around this, we can set the SmoothingMode.AntiAlias or increase the pen’s width(from 1.5f to 2.0f in this case). Setting smoothingmode is recommended.

    // Set the SmoothingMode property to smooth the line.
        e.Graphics.SmoothingMode = 
            System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    If you have any other concern regarding this issue, please feel free to let me know.

    Best regards,

    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, September 15, 2014 8:22 AM
    Moderator
  • thanks for  your reply.

    if you use my code the lines will be broken as follow.

    public Form1()
            {
                InitializeComponent();
                this.ClientSize = new Size(233, 233);
            }
    Maybe the issue is related to the from size.

    Wednesday, September 17, 2014 9:10 AM
  •  Hi Youjun 

    some one mationed "short line". i want to know the theory. Thank you for your information.

    when you use smoothing mode, the graphic is different when drawing a histogram.

    Wednesday, September 17, 2014 9:16 AM
  • Hi Andrew,

    As far as I know, this is a by-design issue. I do think there are some special scenarios will cause the issue. I did more tests on my side, but I found it is really strange.
    1. On all virtual machines, it works very well.
    2. On real devices, some work, but others don’t.

    And I also tried using native programming in C++ to call the DrawLines GDI+ api, but I got same issue. And I also tried to using DrawPath with PathPointType.Line type specified, but run into same issue.

    >>> some one mationed "short line". I want to know the theory.
    I don’t think the short line is the only reason why it doesn’t work, because we can also get it work by increasing width of pen. I do think there are several factors that may cause this issue. So I think that’s why it is a by-design issue. In my opinion, when we draw a curve, in fact we are drawing several “short lines”, so I think that’s why “short line” is mentioned in that thread. And I agree using DrawCurve is more suitable for your case.

    >>> when you use smoothing mode, the graphic is different when drawing a histogram.
    If it is not what you want, I suggest we should not waste time on this. Drawlines seems not a reliable way for all cases. Try DrawCurve as suggested.

    Best regards,
    Youjun Tang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 18, 2014 9:00 AM
    Moderator
  • hi youjun.

    I will keep the broken line. it is the best solution. Althouth it is not perfect.

    You can not draw ines exactly in the other solution.

    but i must give you a advise .before you giving a reply, you should know the question first. in this thread , all the information you offered was mentioned in my question. then this will wasted both you and me a lot time.

    Thursday, September 25, 2014 7:35 AM