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 に答える