有人知道在visual basic 2010中点击图片框控件中像素颜色值的方法吗?
我已经创建了一个小型绘画应用程序,我在小孩游戏中使用但我不希望孩子们在他们着色的图像轮廓上着色。所以要这样做我需要检查当前点击的像素在图片框中不是黑色。
e.g。在伪代码中
if colour not black then Allow pixel to be changed to current colour else Do nothing end if
答案 0 :(得分:1)
这是从PictureBox获取像素的一种方法:
Private Function GetColor(pic As PictureBox, X As Integer, Y As Integer) As Color
If pic Is Nothing Then Return Nothing
Using tmp As New Bitmap(pic.ClientSize.Width, pic.ClientSize.Height)
Dim r As New Rectangle(0, 0, tmp.Width, tmp.Height)
Using g As Graphics = Graphics.FromImage(tmp)
g.DrawImage(pic.Image, r, r, GraphicsUnit.Pixel)
End Using
Return tmp.GetPixel(X, Y)
End Using
End Function
就这样称呼它:
Dim col As Color = GetColor(PictureBox1, someX, someY)