VB.NET绘图的坐标变换

12 篇文章 0 订阅
9 篇文章 0 订阅
这篇博客介绍了如何在.NET环境中进行图形绘制,特别是如何处理坐标原点在左上角与实际需求(左下角)不一致的情况。通过窗体坐标变换和缩放功能,实现了鼠标中键滚动改变绘图比例。代码示例详细展示了如何使用Graphics对象进行坐标转换,包括左右颠倒、垂直翻转等操作,以及绘制线条和标注。
摘要由CSDN通过智能技术生成

.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
   

程序运行测试如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

中游鱼

获取完整源代码,提高工作效率

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值