鼠标悬停时的值高度图像大于图像高度

时间:2013-08-02 04:50:05

标签: c# image height mouse processing

        if (picture != null)
        {
            Int32 h = picture.Height;
            Int32 w = picture.Width;
            Int32 R = picture.GetPixel(e.X, e.Y).R;
            Int32 G = picture.GetPixel(e.X, e.Y).G;
            Int32 B = picture.GetPixel(e.X, e.Y).B;
            lbPixelValue.Text = "R:"+R+ " G:"+G+ " B:"+B;
            lbCoordinates.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);

使用上面的代码,我尝试从图片框上获取鼠标的像素值。

有时这段代码会正常运行,但是其他代码错误:

parameter must to be positive and < height

错误:Int32 R = picture.GetPixel(e.X, e.Y).R;

我尝试调试,问题是Y > image.height

问题是什么,所以我可以获得Y > height图片

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我认为当图片框大于图片时会发生这种情况。 试试这个:

Int32 h = picture.Height;
Int32 w = picture.Width;
if (e.X < w && e.Y < h)
{
    Int32 R = picture.GetPixel(e.X, e.Y).R;
    Int32 G = picture.GetPixel(e.X, e.Y).G;
    Int32 B = picture.GetPixel(e.X, e.Y).B;
    lbPixelValue.Text = "R:" + R + " G:" + G + " B:" + B;
    lbCoordinates.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
}
else
{
    lbPixelValue.Text = "";
    lbCoordinates.Text = "";
}