事前準備
・フォームにPictureBoxを貼り付ける
・PictureBoxのDockプロパティをFillにする
・PictureBoxのBackColorプロパティをWhiteにする
マウスダウン~ムーブにて自由線を描画します。
Public Class Form1
Private bmp As Bitmap
Private StX As Single, StY As Single
Private EnX As Single, EnY As Single
Private mDown As Boolean = False
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
End Sub
Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
mDown = True
StX = e.X : StY = e.Y
EnX = e.X : EnY = e.Y
End Sub
Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If mDown = False Then Exit Sub
EnX = e.X : EnY = e.Y
Dim g As Graphics = Graphics.FromImage(bmp)
g.DrawLine(Pens.Black, StX, StY, EnX, EnY)
StX = e.X : StY = e.Y
g.Dispose()
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
mDown = False
End Sub
Private Sub Picture_Box(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(bmp, 0, 0, PictureBox1.Width, PictureBox1.Height)
End Sub
End Class
こんな感じになります。
関連記事
・Bitmapに自由線を描画し表示する(ver2)
・Graphics.DrawLineメソッド
・Bitmapクラス
システム開発のためのVB.NETプログラミング関係一覧に戻る