.NET绘图中,坐标原点(0,0)在左上角,而我们实际运用中,坐标在左下角,这就要进行窗体坐标变换。
通过鼠标中键滚动轮,可以改变绘图比例。
Dim ZoomRate As Double = 1
Private Sub PictureBox1_MouseWheel(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseWheel
If e.Delta > 0 Then
ZoomRate += 0.01 '滚动 1 格 加0.01
Else
ZoomRate -= 0.01
End If
PicDraw(ZoomRate)'鼠标中键滚动,改变放大倍率
' Console.WriteLine(ZoomRate)
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
PicDraw(ZoomRate)
End Sub
Private Sub PicDraw(Optional ByRef ZoomRate As Single = 1)
'Dim PictureBox1 As New PictureBox
'Controls.Add(PictureBox1) '添加控件到窗体
'PictureBox1.Dock = DockStyle.Fill
Dim gr As Graphics = PictureBox1.CreateGraphics
Dim RP As New Pen(Brushes.Red, 2) '红色笔
Dim BP As New Pen(Brushes.Blue, 2) '蓝色笔
Dim BKP As New Pen(Brushes.Black, 2) '蓝色笔
PictureBox1.Refresh() '重新显示
gr.ResetTransform()
gr.ScaleTransform(ZoomRate, ZoomRate) '1:1变换X,Y轴
Dim W = gr.MeasureString("原点在左上角", Me.Font).Width - 3 '测量字体的高
Dim h = gr.MeasureString("原点在左上角", Me.Font).Height '测量字体的高
'----------------------------------------------------------------------------蓝色区坐标原点左上角
gr.DrawString("原点在左上角", Font, Brushes.Blue, 50, 50)
gr.MultiplyTransform(New Matrix(-1, 0, 0, 1, 0, 0)) '左右颠倒
gr.DrawString("原点在左上角", Font, Brushes.Blue, -50 - W, 50 + h)
gr.ResetTransform()
gr.ScaleTransform(ZoomRate, ZoomRate) '1:1变换X,Y轴
RP.EndCap = LineCap.ArrowAnchor
BP.EndCap = LineCap.ArrowAnchor
gr.DrawLine(BP, New Point(0, 0), New Point(200, 0))
gr.DrawLine(BP, New Point(0, 0), New Point(0, 200))
gr.DrawString("X轴", Me.Font, Brushes.Blue, New Point(200, 0))
gr.DrawString("Y轴", Me.Font, Brushes.Blue, New Point(0, 200)) '未作处理,目的是看清坐标是怎么变换
BP.EndCap = LineCap.NoAnchor
gr.DrawLine(BP, New Point(0, 0), New Point(200, 200))
gr.DrawString("线(200,200)", Me.Font, Brushes.Blue, New Point(200, 200))
'---------------------------------------------------------------------------黑色区坐标原点左上角
gr.ResetTransform()
gr.ScaleTransform(ZoomRate, ZoomRate) '1:1变换X,Y轴
gr.DrawString("Y轴", Me.Font, Brushes.Black, New Point(300, PictureBox1.Height - 200 - h))
gr.DrawString("X轴", Me.Font, Brushes.Black, New Point(300 + 200, PictureBox1.Height - h))
BKP.EndCap = LineCap.ArrowAnchor
gr.DrawLine(BKP, New Point(300, PictureBox1.Height), New Point(300, PictureBox1.Height - 200))
gr.DrawLine(BKP, New Point(300, PictureBox1.Height), New Point(300 + 200, PictureBox1.Height))
BKP.EndCap = LineCap.NoAnchor
gr.DrawLine(BKP, New Point(300, PictureBox1.Height), New Point(300 + 200, PictureBox1.Height - 200))
gr.DrawString("线(300,200)", Me.Font, Brushes.Black, New Point(300 + 200, PictureBox1.Height - 200 - h))
gr.DrawString("原点在左上角", Font, Brushes.Black, 300 + 50, PictureBox1.Height - 50 - h)
'-------------------------------------------------------------------------红色区坐标原点左下角
gr.ResetTransform()
gr.TranslateTransform(0, PictureBox1.Height) '以PictureBox1高,作为原点,即左下角为原点
gr.ScaleTransform(ZoomRate, -1 * ZoomRate) '1:1变换X,Y轴
RP.EndCap = LineCap.ArrowAnchor
gr.DrawLine(RP, New Point(0, 0), New Point(200, 0))
gr.DrawLine(RP, New Point(0, 0), New Point(0, 200))
gr.DrawString("X轴", Me.Font, Brushes.Red, New Point(200, 0))
gr.DrawString("Y轴", Me.Font, Brushes.Red, New Point(0, 200)) '未作处理,目的是看清坐标是怎么变换
gr.DrawString("原点在左下角", Font, Brushes.Red, 50, 50)
h = gr.MeasureString("Y轴", Me.Font).Height '测量字体的高
W = gr.MeasureString("Y轴", Me.Font).Width + 2
gr.MultiplyTransform(New Matrix(1, 0, 0, -1, 0, 0)) '坐标矩阵转换,垂直颠倒, 并垂直平移 h
gr.DrawString("Y轴正常", Me.Font, Brushes.DarkRed, New Point(W, -200 - h))
gr.DrawString("X轴正常", Me.Font, Brushes.DarkRed, New Point(200 + W, 0 - h))
gr.DrawString("原点在左下角", Font, Brushes.Red, 50, -50)
RP.EndCap = LineCap.NoAnchor
gr.DrawLine(RP, New Point(0, 0), New Point(200, -200))
gr.DrawString("线(200,200)", Me.Font, Brushes.Red, New Point(200, -200))
gr.DrawString("Y轴正常文字", Me.Font, Brushes.DarkRed, New Point(300, 200 - PictureBox1.Height))
gr.DrawString("X轴正常文字", Me.Font, Brushes.DarkRed, New Point(300 + 200, 0 - PictureBox1.Height))
gr.DrawString("原点在左下角", Font, Brushes.Red, 300 + 50, 50 - PictureBox1.Height)
RP.EndCap = LineCap.NoAnchor
gr.DrawLine(RP, New Point(300, 0 - PictureBox1.Height), New Point(300 + 200, 0 - PictureBox1.Height))
gr.DrawLine(RP, New Point(300, 0 - PictureBox1.Height), New Point(300, 200 - PictureBox1.Height))
gr.DrawLine(RP, New Point(300, 0 - PictureBox1.Height), New Point(300 + 200, 200 - PictureBox1.Height))
gr.DrawString("线(200,200)", Me.Font, Brushes.Red, New Point(300 + 200, 200 - PictureBox1.Height))
gr.ResetTransform() '重置坐标系还原
End Sub
程序运行测试如下: