Bitmapに自由線を描画し表示する(ver2)

事前準備などはこちら


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.FromHwnd(PictureBox1.Handle)
  g.DrawLine(Pens.Black, StX, StY, EnX, EnY)
  g.Dispose()
  g = Graphics.FromImage(bmp)
  g.DrawLine(Pens.Black, StX, StY, EnX, EnY)
  StX = e.X : StY = e.Y
  g.Dispose()
 End Sub

 Private Sub PictureBox_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
  mDown = False
  PictureBox1.Invalidate()
 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に自由線を描画し表示する



システム開発のためのVB.NETプログラミング関係一覧に戻る


Google検索の結果

system-development