Always problems with LineDash and DrawLine

Bonjour,

I try to replace my own routines to draw dash lines with g.LineDash
But g.LineDash is always affected by g.PenSize
A workaround is to divide the dash values by g.PenSize
But with g.DrawLine, dashes don’t start correctly.
And continuous lines drawn with DrawLine are not correctly axed.
The 2 last problems don’t exist with DrawPath.
Example :


Code :

g.LineCap = Graphics.LineCapTypes.■■■■
g.PenSize = 0.5
g.DrawLine(0, 20, me.Width, 20)
g.PenSize = 10
g.DrawLine(20, 20, me.Width/2 - 10, 20)    //<= Line is not axed on y = 20
Var mPath As New GraphicsPath
mPath.MoveToPoint(me.Width/2 + 10, 20)
mPath.AddLineToPoint(me.Width - 20, 20)
g.DrawPath(mPath)                         //<= Line is axed on y = 20

Var dashValue As Double = 20.0
g.PenSize = 1
g.LineDash = Array(dashValue, dashValue)  //<= dashValue is affected by PenSize
g.DrawLine(0, 50, me.Width, 50)
g.PenSize = 5
g.DrawLine(0, 60, me.Width, 60)
g.PenSize = 2
g.DrawLine(0, 75, me.Width, 75)

g.PenSize = 1
g.LineDash = Array(dashValue, dashValue)  //<= dashValue corrected but not aligned with DrawLine
g.DrawLine(0, 100, me.Width, 100)
g.PenSize = 5
g.LineDash = Array(dashValue / g.PenSize, dashValue / g.PenSize)
g.DrawLine(0, 110, me.Width, 110)
g.PenSize = 2
g.LineDash = Array(dashValue / g.PenSize, dashValue / g.PenSize)
g.DrawLine(0, 125, me.Width, 125)


g.PenSize = 1
g.LineDash = Array(dashValue, dashValue) //<= dashValue corrected AND aligned with DrawPath
Var mPath2 As New GraphicsPath
mPath2.MoveToPoint(0, 150)
mPath2.AddLineToPoint(me.Width, 150)
g.DrawPath(mPath2)
g.PenSize = 5
g.LineDash = Array(dashValue / g.PenSize, dashValue / g.PenSize)
Var mPath3 As New GraphicsPath
mPath3.MoveToPoint(0, 160)
mPath3.AddLineToPoint(me.Width, 160)
g.DrawPath(mPath3)
g.PenSize = 2
g.LineDash = Array(dashValue / g.PenSize, dashValue / g.PenSize)
Var mPath4 As New GraphicsPath
mPath4.MoveToPoint(0, 170)
mPath4.AddLineToPoint(me.Width, 170)
g.DrawPath(mPath4)

DrawPath is the way to go. PenSize effect is prevedibile and correct

Bonjour Eric! Yes, unfortunately the Graphics class has those peculiarities, but at least the workarounds are easy and the outcome is consistent.

  1. LineDash: I found no other solution than dividing the dash lengths by g.PenSize.
  2. Related to n. 3: translate the X,Y values of the start and end points of the line by half pen size. This is what I use directly before drawing the line with g.DrawLine:

var hP As Double = line_Size/2
X1 = X1 - hp
Y1 = Y1 - hp
X2 = X2 - hp
Y2 = Y2 - hp

Merci Antonio et Andrea !