0
        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图片

我该如何解决?

4

1 回答 1

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 = "";
}
于 2013-08-02T05:28:05.777 回答