2017年12月9日 星期六

如何在圖像中繪製圖形



如何在圖像中繪製圖形


要在圖像中繪製圖形,必須先以圖像建立記憶體畫布 ( 例如:bmp),再透過兩緩衝技術,將畫製在 記憶體畫布 bmp 的圖形以 g.DrawImage(bmp, 0, 0)   顯示在畫布上,其中     g = PictureBox1.CreateGraphics() 是實體的畫布。
源自 http://blog.e-happy.com.tw/?p=560



"google.bmp" ---放在C:\Users\User\Documents\Visual Studio 2010\Projects\WindowsApplication7\WindowsApplication7\bin\Debug



Public Class Form1
    '參考文件:Visual Basic 2010 程式設計速學對策--ch13 page 26 ,DrawImage2.sln
    Dim g, Draw As Graphics '畫布
    Dim pen As Pen = New Pen(Color.Blue, 2)
    Dim point As Point
    Dim bmp As Bitmap

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        point = New Point(e.X, e.Y)  '記憶直線開始的位置
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = MouseButtons.Left Then '按滑鼠左鍵
            g.DrawImage(bmp, 0, 0)           '在表單上顯示 bmp 記憶體圖像
            g.DrawLine(pen, point.X, point.Y, e.X, e.Y) '在表單上繪提示的直線
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Draw.DrawLine(pen, point.X, point.Y, e.X, e.Y)   '在記憶體畫布 Draw 繪直線

    End Sub

    Private Sub initinal()
        bmp = New Bitmap("google.bmp") '以 s04.bmp 檔為記憶體畫布,圖檔大小最好先控制好
        PictureBox1.Image = bmp     '以  PictureBox1 為實體畫布
        PictureBox1.Height = bmp.Height
        PictureBox1.Width = bmp.Width
        PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
        g = PictureBox1.CreateGraphics()
        Draw = Graphics.FromImage(bmp)    '以 bmp 建立 記憶體畫布 Draw
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        initinal()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

   Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        initinal()
    End Sub

End Class

沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...