vb.net 如何在 Visual Basic 2010 中获取图片框控件中像素的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17687719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:17:33  来源:igfitidea点击:

How do i get the colour of a pixel in a picturebox control in visual basic 2010

vb.netvisual-studio-2010

提问by Dean

Does anyone know a way to get the colour value of a pixel in a picturebox control when it is clicked in visual basic 2010?

有没有人知道在visual basic 2010中单击时获取图片框控件中像素颜色值的方法?

I have created a small paint application which im using in a kiddies game but I do not want the children to colour over the outline of the image they are colouring in. So to do so I need to check if the currently clicked pixel in the picturebox is not black.

我创建了一个小型绘画应用程序,我在儿童游戏中使用它,但我不希望孩子们在他们正在着色的图像的轮廓上着色。因此,我需要检查图片框中当前单击的像素不是黑色的。

e.g. in pseudo code

例如在伪代码中

if colour not black then
    Allow pixel to be changed to current colour 
else
     Do nothing 
end if

回答by

This is one way you can get a pixel from PictureBox:

这是您可以从 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

Just call it like this:

就这样称呼它:

Dim col As Color = GetColor(PictureBox1, someX, someY)