通过像素BMP C#的例外情况

Sud*_*ha 3 c#

我使用以下代码来通过BMP的像素

在此输入图像描述

for (int i = 0; i <= Image.Width; i++)
 {
    for (int j = 0; j <= Image.Height; j++)
    {
              color = Image.GetPixel(i, j); //get 
    }

 }
Run Code Online (Sandbox Code Playgroud)

但我得到一个例外

System.ArgumentOutOfRangeException was unhandled
  Message="Parameter must be positive and < Height.\r\nParameter name: y"
  Source="System.Drawing"
  ParamName="y"
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我得到这个..我使用BMP有效的高度和相同的代码与硬编码值正常工作

@Odded

No:1显示我需要什么,没有2是你的代码有什么想法吗?

在此输入图像描述

Ode*_*ded 5

循环中有一个off-by-one错误.

如果图像HeightWidth是100,拿到"最后"像素,您将需要调用它的GetPixel(99,99).

for (int i = 0; i < Image.Width; i++)
 {
    for (int j = 0; j < Image.Height; j++)
    {
              color = Image.GetPixel(i, j); //get 
    }

 }
Run Code Online (Sandbox Code Playgroud)


Ste*_*ert 5

只需改变高度和宽度.这是一个在你自己的代码中看得太远的例子 - 这带回了这么多的回忆......

for(int i=0;i<BMP.Height;i++)
{
   for(int j=0;j<BMP.Width;j++)
   {
      color = BMP.GetPixel(j,i);    
   }
}
Run Code Online (Sandbox Code Playgroud)