2014-09-01 91 views
0

我有一個顏色選擇器,我使用的圖片與MouseMove而不是Click事件,以便允許顏色顯示「即時」,但是當我離開了界限的圖像,我得到一個錯誤,不能想到處理它的最佳方式。動態顏色選擇器在vb.net

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove 
     If isWithin(DirectCast(sender, PictureBox), New Point(e.X, e.Y)) And e.Button = Windows.Forms.MouseButtons.Left Then 
      Dim bmptmp As New Bitmap(Me.PictureBox1.Image, Me.PictureBox1.Width, Me.PictureBox1.Height) 
      Dim clrtmp As Color = bmptmp.GetPixel(e.X, e.Y) '<--- error here 
      ... 
      ... 
     End If 
End Sub 

我寫的isWithin功能,並把它添加到檢查的If上面一個可能的方式,但是這似乎沒有工作:

Public Function isWithin(pic As PictureBox, pnt As Point) 
    Dim rct1 As New Rectangle(pic.Top, pic.Left, pic.Width, pic.Height) 
    Dim rct2 As New Rectangle(pnt.X - 1, pnt.Y - 1, 1, 1) 
    Return rct1.IntersectsWith(rct2) 
End Function 

錯誤我收到是

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Drawing.dll - Additional information: Parameter must be positive and < Height 

(或< Width如果我走得太遠)。我認爲這是因爲e.Xe.Y不再大於0或小於高度或寬度(這正是我試圖用isWithin函數檢查的結果)

+0

可能很高興知道*哪個*錯誤。你可能會有其他人在場,因爲那裏有一些資源可能沒有妥善處理。 – Plutonix 2014-09-01 15:01:56

+0

@Plutonix啊,是的,將在一分鐘內重新創建錯誤。正如我已經解決它,我忘了包括在帖子的末尾 – bmgh1985 2014-09-01 15:10:21

+0

@Plutonix現在補充說,錯誤信息 – bmgh1985 2014-09-01 15:22:15

回答

0

因爲我輸入的是問題但認爲我仍然會添加它。封閉它在Try... Catch ex as Exception... End Try然後它工作正常。沒有必要真正處理錯誤,因爲它是一個預期的錯誤,只是我being and不馴,並從VBA思想系列中得到它(「Do ...或者不要,沒有嘗試。」 - Yoda & VBA)

+0

也這使得我的'isWithin'功能是多餘的,所以已經從最終的代碼中刪除了它 – bmgh1985 2014-09-01 15:11:24