如何使用xy坐标缩放和设置绘图图片框的原点

时间:2015-11-06 21:38:34

标签: vb.net visual-studio

我有一系列由三角测量方法生成的X,Y坐标。我想在图片框上的X,Y坐标上绘制线条,以便验证数据条目。我的代码生成这些x,y值以动态创建文本框,然后在图片框上绘制它们。目前它正在工作,但结果线非常小,并且它们不居中。我怎样才能解决这个问题? picturebox1是500x500。这是代码

 Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    If Me.CheckBox1.Checked = True Then
        Dim n As Integer
        Dim g As Graphics
        g = PictureBox1.CreateGraphics

        Dim x, y, x1, y1 As String
        x = Me.Controls.Item("myTextBoxX1").Text
        y = Me.Controls.Item("myTextBoxY1").Text
        x1 = Me.Controls.Item("myTextBoxX2").Text
        y1 = Me.Controls.Item("myTextBoxY2").Text

        Dim xi, yi, x1i, y1i As Integer

        xi = Convert.ToDecimal(x)
        yi = Convert.ToDecimal(y)
        x1i = Convert.ToDecimal(x1)
        y1i = Convert.ToDecimal(y1)

        g.DrawLine(New Pen(ForeColor), xi, yi, x1i, y1i)
        Dim x1a, y1a, x2a, y2a As String
        Dim x1ai, y1ai, x2ai, y2ai As Integer
        For n = 3 To Me.TextBoxABPts.Text
            x1a = Me.Controls.Item("myTextBoxX" & n - 1).Text
            y1a = Me.Controls.Item("myTextBoxY" & n - 1).Text
            x2a = Me.Controls.Item("myTextBoxX" & n).Text
            y2a = Me.Controls.Item("myTextBoxY" & n).Text



            x1ai = Convert.ToDecimal(x1a)
            y1ai = Convert.ToDecimal(y1a)
            x2ai = Convert.ToDecimal(x2a)
            y2ai = Convert.ToDecimal(y2a)

            g.DrawLine(New Pen(ForeColor), x1ai, y1ai, x2ai, y2ai)
        Next n

        g.DrawLine(New Pen(ForeColor), x2ai, y2ai, xi, yi)
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

使用.Net Point可能更容易使用坐标。它们由X和Y整数值组成。 PointF是另一种基于单身的结构。要么适合你。创建一个可以转换为比例坐标的函数。

Private Function ScaledPoint(Point As Point, Origin As Point, Optional Scale As Double = 1.0) As Point
    Return New Point(CInt(Origin.X + Point.X * Scale), CInt(Origin.Y + Point.Y * Scale))
End Function

在您的应用程序中,定义原点和比例因子。例如:

Dim origin As New Point(200, 100) 'Moves all points 200 right, 100 down.
Dim scaleFactor As Double = 5 'Makes drawing 5 times larger.

现在,您调用g.DrawLine,将其更改为:

Dim Point1 As New Point(x1ai, y1ai) 'There are lots of ways to do this.
Dim Point2 As New Point(x2ai, y2ai) 'This is only an example.
g.DrawLine(New Pen(ForeColor), ScaledPoint(Point1, origin), ScaledPoint(Point2, origin))

有很多方法可以定义Point1和Point2。