获取像素方法中的超出范围异常

本文关键字:范围 异常 像素 方法 获取 | 更新日期: 2023-09-27 18:37:13

当 for 循环达到 [0,240] 时,我正在尝试使用 getPixel 方法将图像的像素值存储到 2-D 数组中我得到超出范围的异常,有人可以帮助我吗?

  // Loop through the images pixels to store in array. 
                for (x = 0; x < image1.Height; x++)
                {
                    for (y = 0; y < image1.Width; y++)
                    {
                       Color p = ((Bitmap)image1).GetPixel(x, y);
                         pic[x,y] = p.ToString();
                    }
                }

获取像素方法中的超出范围异常

您循环x作为高度,y作为宽度,但随后您以另一种方式使用它们来访问像素。

循环x宽度,y为高度:

// Loop through the images pixels to store in array. 
            for (y = 0; y < image1.Height; y++)
            {
                for (x = 0; x < image1.Width; x++)
                {
                   Color p = ((Bitmap)image1).GetPixel(x, y);
                     pic[x,y] = p.ToString();
                }
            }