Search code examples
.netzoomingbasicdrawing2d

Linewidth increases when zooming in on vector drawing .NET


When zooming in on a vector drawing in Visual Basic .NET, after a certain point the lines get thicker with each zoom action.

App functionality is basically to draw some lines:

  • a graphics matrix is set using transformation and scaling to get the whole drawing fill the canvas (using g.Transform = myMatrix)
  • lines are drawn (using "g.DrawLine(...)")

... and when zooming in:

  • the transformation matrix is adjusted to zoom in on the mouse pointer location
  • the scalefactor for the matrix increases by a factor 1.1

The matrix is set in the PrepareDrawing method:

Private Function PrepareDrawing(ByRef g As Graphics) As Matrix
    'Create transformation matrix
    Dim myMatrix = New Matrix

    'Determine the zoom factor
    If zoomfactor > 10000 Then zoomfactor = 10000
    If zoomfactor < 0.001 Then zoomfactor = 0.001

    scaleFactor = zoomfactor * BORDERFACTOR * Math.Min(pb.Width / viewportModel.Width, pb.Height / viewportModel.Height)

    'Determine the offset to keep everything centered
    If panningOffset.IsEmpty Then
        ZoomToNetwork()
    End If

    myMatrix.Translate(panningOffset.X, panningOffset.Y)
    myMatrix.Scale(scaleFactor, -scaleFactor, MatrixOrder.Append)

    'transform current graphics
    g.Transform = myMatrix

    Return myMatrix
End Function

And the zooming functionality is set in the ZoomInWithMouseWheel method:

 Public Sub ZoomInWithMouseWheel(ByVal e As Point, ByVal delta As Single)
    Dim orgZoomfactor = zoomfactor
    Dim screenX As Double = e.X 
    Dim screenY As Double = e.Y 

    If delta > 0 Then
        zoomfactor = zoomfactor * 1.1 
    Else
        zoomfactor = zoomfactor / 1.1 
    End If

    scaleFactor = zoomfactor * BORDERFACTOR * Math.Min(pb.Width / viewportModel.Width, pb.Height / viewportModel.Height)

    Dim perc = zoomfactor / orgZoomfactor
    panningOffset.Offset(-(perc * screenX - screenX) / scaleFactor, (perc * screenY - screenY) / scaleFactor)

End Sub

It turns out that when the first element of the matrix becomes larger than 1, the line width increases with this number.

Any thoughts on what is happening and how I can prevend the line width from increasing?


Solution

  • Okay, found the answer. Line width should be compensated for the matrix element(0), e.g.:

    g.DrawLines(New Pen(Color.Blue, lineWidth / g.Transform.Elements(0)), pnts)
    

    where lineWidth is 1 for example