winforms - Drawing multiple lines with DrawLines and DrawLine produces different result -
i trying draw multiple lines on winforms panel using it's graphics object in paint event. drawing number of lines joining given points. so, first of did this,
private void panel1_paint(object sender, painteventargs e) { e.graphics.drawlines(new pen(new solidbrush(color.crimson), 3), pointfs.toarray()); float width = 10; float height = 10; var circlebrush = new solidbrush(color.crimson); foreach (var point in pointfs) { float rectanglex = point.x - width / 2; float rectangley = point.y - height / 2; var r = new rectanglef(rectanglex, rectangley, width, height); e.graphics.fillellipse(circlebrush, r); } }
which produces result image below,
as can see lines drawn having little bit of extension @ sharp turns, not expected. so, changed drawlines code to,
var pen = new pen(new solidbrush(color.crimson), 3); (int = 1; < pointfs.count; i++) { e.graphics.drawline(pen, pointfs[i - 1], pointfs[i]); }
and drawing works fine.
can tell difference between 2 approaches?
i have had same problem (stumbled upon question during research), have found solution.
the problem caused linejoin property on pen used. devx page explains different linejoin types (see figure 1 illustrations). seems miter default type, , causes "overshoot" when have sharp angles.
i solved problem setting linejoin property bevel:
var pen = new pen(new solidbrush(color.crimson), 3); pen.linejoin = drawing2d.linejoin.bevel;
now drawlines no longer overshoot points.
Comments
Post a Comment