摘要:Drawing in PictureBox
Drawing in PictureBox
當用Image.FromFile 時, 因為會lock file,所以就不用這個了
於是改用Image.FromStream
Dim lbPicture As Bitmap
Dim lgGraphics As Graphics
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lbPicture = New Bitmap(picBoxDraw1.Width, picBoxDraw1.Height)
lgGraphics = Graphics.FromImage(lbPicture)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoxDraw1.MouseMove
Try
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim liX As Integer = MousePosition.X - picBoxDraw1.Location.X - Me.Location.X - Me.TabControl1.Location.X - Me.PanelPic.Location.X
Dim liY As Integer = MousePosition.Y - picBoxDraw1.Location.Y - Me.Location.Y - Me.TabControl1.Location.Y - Me.PanelPic.Location.Y
Me.lgGraphics.FillEllipse(New SolidBrush(System.Drawing.Color.Red), liX, liY, 5, 5)
picBoxDraw1.Image = lbPicture
End If
Catch ex As Exception
End Try
End Sub 
Private Sub btnPicSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPicSave.Click
Try
Dim SaveFileDialogPic1 As New SaveFileDialog
SaveFileDialogPic1.Filter = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*"
If (SaveFileDialogPic1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
picBoxDraw1.Image.Save(SaveFileDialogPic1.FileName)
End If
Catch ex As Exception
MsgBox("bug")
End Try
End Sub
Private Sub btnPicLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPicLoad.Click
Try
Dim OpenFileDialogPic1 As New OpenFileDialog
OpenFileDialogPic1.Filter = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*"
If (OpenFileDialogPic1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Dim ms As New IO.MemoryStream
Dim fs As IO.FileStream = New IO.FileStream(OpenFileDialogPic1.FileName, IO.FileMode.Open)
Dim iFilelength As Integer = CInt(fs.Length - 1)
Dim arrBytes(iFilelength) As Byte
fs.Read(arrBytes, 0, iFilelength)
fs.Close()
ms.Write(arrBytes, 0, iFilelength)
picBoxDraw1.Image = Image.FromStream(ms)
lbPicture = CType(picBoxDraw1.Image, Bitmap)
lgGraphics = Graphics.FromImage(lbPicture)
OpenFileDialogPic1.Dispose()
End If
Catch ex As Exception
End Try
End Sub
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life
Private
Private