我有一個「完美」的工作代碼,用於將部分屏幕放入方形PictureBox。
我有一個「完美」的工作代碼,用筆在畫框上繪圖。
我完全不明白,爲什麼我無法在畫框中的位圖上畫筆 - 它只能在灰色背景上繪製。
該解決方案(也是按谷歌)看似簡單,但任何嘗試從PictureBox.Image獲取gg圖形的方法都會導致(「創建表單時發生錯誤,詳情請參閱Exception.InnerException。錯誤是:Value不能爲空 參數名稱:image「和」使用'New'關鍵字創建對象的新實例...「)。從PictureBox獲取位圖 - 對象的新實例
我覺得麻煩的是在我定義了一個子外的位圖作爲私人的方式,但我不能找出正確的觀念......
Private bit As New System.Drawing.Bitmap(250, 250)
Private gg As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' ^^^^^^^^^^ HERE IS THE PROBLEM ^^^^^^^^^^^^^^^^^^^^^^
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Private br As New Pen(Color.Red)
Private dwn As Boolean
Private lst As Point
Private firstrun As Boolean = True
Dim Tloustka As Integer = 10
Dim Barva As Integer
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'gg = Graphics.FromImage(Me.PictureBox1.Image)
dwn = True
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If dwn = True Then
Dim s As Integer
Dim xy As Point
Dim br2 As New SolidBrush(Color.FromName("White"))
s = Tloustka
br.Color = Color.FromName("White")
br.Width = Tloustka
xy.X = e.X
xy.Y = e.Y
If firstrun = True Then
lst = xy
firstrun = False
End If
gg.FillEllipse(br2, xy.X - CLng(s/2), xy.Y - CLng(s/2), s, s)
gg.DrawLine(br, lst, xy)
lst = xy
PictureBox1.Image = bit
End If
End Sub
etc. etc. etc.
刪除「位」。當你完成繪圖時,調用PictureBox1.Invalidate(),以便知道它需要重新繪製圖像。 –