VB.net 繪圖代碼

來源:互聯網
上載者:User

Imports System
Imports System.IO
Public Class Form2
    Private mBmpBuffer As Bitmap
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mBmpBuffer = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height, Me.PictureBox1.CreateGraphics)

        Dim g As Graphics = Graphics.FromImage(mBmpBuffer)

        Dim i As Integer
        Dim aa(3, 1) As Single
        Dim stra() As String
        Try
            Dim sr As StreamReader = New StreamReader("D:/模型相關/modeldesign/modelpaint/data.csv")
            Dim line As String
            Do
                line = sr.ReadLine()

                i = i + 1
                If i > 1 Then
                    stra = Split(line, ";")
                    aa(i - 2, 0) = Val(stra(0))
                    aa(i - 2, 1) = Val(stra(1))
                End If

            Loop Until line Is Nothing
            sr.Close()
        Catch ex As Exception

        End Try

        Dim ix As Single
        Dim iy As Single
        Dim temp As Single

        temp = aa(0, 0)
        For i = 0 To 3
            If aa(i, 0) > temp Then
                temp = aa(i, 0)
            End If
        Next
        ix = temp

        temp = aa(0, 1)
        For i = 0 To 3
            If aa(i, 1) > temp Then
                temp = aa(i, 1)
            End If
        Next
        iy = temp
        '變換座標
        Dim bx As Single
        Dim by As Single

        bx = PictureBox1.Width / ix
        by = PictureBox1.Height / iy

        'Dim g As Graphics = Me.PictureBox1.CreateGraphics
        g.Clear(Color.White)
        Dim mypoint(3) As Point
        Dim n As Integer = 3
        '變換座標原點
        g.TranslateTransform(PictureBox1.Width / 15, PictureBox1.Height * 14 / 15)
        'g.ScaleTransform(PictureBox1.Width / ix, PictureBox1.Height / iy)

        '繪製座標軸
        Dim mypen As New Pen(Color.Black)
        Dim sb As New SolidBrush(Color.Black)
        'Dim sb1 As Brush = Brushes.Red
        Dim mypen1 As New Pen(Color.Blue, 6)
        g.DrawLine(mypen, 0, 0, PictureBox1.Width - 40, 0)
        g.DrawLine(mypen, 0, 0, 0, 20 - PictureBox1.Height)
        For i = 0 To PictureBox1.Width - 40 Step 40
            g.DrawLine(mypen, i, 0, i, -10)
            g.DrawString(Int(i / 0.9 / bx), New Font("宋體", 8), sb, i - 10, 0)
        Next
        g.DrawString("C", New Font("宋體", 8), sb, PictureBox1.Width - 50, 0)

        For i = 40 To PictureBox1.Height - 40 Step 40
            g.DrawLine(mypen, 0, -i, 10, -i)
            g.DrawString(Int(i / 0.9 / by), New Font("宋體", 8), sb, -20, -i - 10)
        Next
        g.DrawString("x", New Font("宋體", 8), sb, -10, 20 - PictureBox1.Height)
        'mypen.Width = 1
        g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        For i = 0 To n
            mypoint(i).X = aa(i, 0) * bx * 0.9
            mypoint(i).Y = -aa(i, 1) * by * 0.9
            'g.FillRectangle(sb1, mypoint(i).X, mypoint(i).Y, 3, mypoint(i).Y)
            g.DrawLine(mypen1, mypoint(i).X, 0, mypoint(i).X, mypoint(i).Y)
        Next

        '開始畫線

 

        'g.DrawCurve(mypen, mypoint)

        Me.PictureBox1.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim strFilter As String = "BMP檔案(*.bmp)|*.bmp"
        Dim pdlg As SaveFileDialog = New SaveFileDialog
        pdlg.Title = "地圖另存新檔"
        pdlg.Filter = strFilter
        pdlg.OverwritePrompt = True
        pdlg.ShowDialog()

        If Not pdlg.FileName.Equals("") Then
            If Not (mBmpBuffer Is Nothing) Then
                'mBmpBuffer.Save("c:/a.bmp")
                mBmpBuffer.Save(pdlg.FileName)
            End If

        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If Not (mBmpBuffer Is Nothing) Then
            e.Graphics.DrawImage(mBmpBuffer, 0, 0)
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Clear()
        ComboBox1.Items.Add("模型一")
        ComboBox1.Items.Add("模型二")
        ComboBox1.Items.Add("模型三")
        ComboBox1.Text = "模型一"
    End Sub
End Class

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.