Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown 'ShownはForm1が初めて表示されるときに1回だけ実行されます。 'このタイミングでPictureBoxとBitmapを関連付けます。 '引数は描画領域の幅と高さ canvas = New Bitmap(PictureBox1.Width, PictureBox1.Height) PictureBox1.Image = canvas End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '関連付けられたBitmapに描画します。 Dim g As Graphics = Graphics.FromImage(canvas) g.DrawLine(Pens.Black, a, b, c, d) 'Bitmapの内容が変わってもPictureBox1は自動的に気が付いてくれないので、 '一旦現状の表示をリセットして関連付けられたBitmapを表示してもらいます。 PictureBox1.Invalidate() End Sub
Private Lines As New List(Of Rectangle) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Lines.Add(New Rectangle(a, b, c, d)) PictureBox1.Invalidate() End Sub Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint For Each line As Rectangle In Lines e.Graphics.DrawLine(Pens.Black, line.X, line.Y, line.X + line.Width, line.Y + line.Height) Next End Sub