1

I'm trying to draw straight vertical lines with pen width of 1. The following method draws vertical grid lines of a line chart.

public override void DrawGridLines(GDIGraphics graphics, Rectangle plotArea)
{
    using (Pen pen = new Pen(gridLineColor, gridLineThickness))
    {
        float x = plotArea.Right - 1;
        for (int i = 0; i < niceIntervalCount; i++)
        {
            int roundedX = Mathf.RoundToInt(x);
            graphics.DrawLine(pen, roundedX, plotArea.Top, roundedX, plotArea.Bottom - 2);
            x -= niceIntervalSpacing;
        }
    }
}

gridLineThickness is set to 1. I'm passing integer parameters to DrawLine method to prevent subpixel rendering. I also disabled antialiasing and set graphics.PageUnit to GraphicsUnit.Pixel. In one cases, vertical lines are drawn with thickness of 2 (usually, when many lines).

enter image description here

In other cases, this bug disappears.

enter image description here

Click the images above to see them full size without distortion.

Paviel Kraskoŭski
  • 1,429
  • 9
  • 16
  • GDIGraphics class is a part of Aurigma.GraphicsMill.Drawing ? System.Drawing doesn't seem to contain GDIGraphics .. you override DrawGridLines from which class ? – Xavave Feb 06 '18 at 12:51
  • @Xavave `GDIGraphics` is a synonym of `System.Drawing.Graphics`. I'm overriding `DrawGridLines` from my own class. – Paviel Kraskoŭski Feb 06 '18 at 12:55
  • I've tried to reproduce your issue in a winform picturebox, but no success : every time I redraw the grid lines, the width is fine (1 pixel wide); I also set Application.SetCompatibleTextRenderingDefault(true); to disable AntiAlising and set graphics.PageUnit to GraphicsUnit.Pixel as you said – Xavave Feb 06 '18 at 13:31
  • That RoundToInt() call does nothing useful right now and if you "disabled antialiasing" then there is no evidence of that in the screenshots. Change the Graphics.PixelOffsetMode property, the default is good for drawing images but not for line drawing. – Hans Passant Feb 06 '18 at 14:19

0 Answers0