穿越像素BMP C#的例外情况
我使用以下代码来通过BMP的像素
for (int i = 0; i <= Image.Width; i++) { for (int j = 0; j <= Image.Height; j++) { color = Image.GetPixel(i, j); //get } }
但我得到一个例外
System.ArgumentOutOfRangeException was unhandled Message="Parameter must be positive and < Height.\r\nParameter name: y" Source="System.Drawing" ParamName="y"
我不知道为什么我得到这个..即时通讯使用具有有效高度的BMP
和相同的代码与硬编码值正常工作
@Odded
No:1显示我需要什么,没有2是什么发生你的代码任何想法?
只需改变高度和宽度。 这是一个在你自己的代码中看得太远的例子 – 这带回了这么多的回忆……
for(int i=0;i
循环中有一个off-by-one错误。
如果图像Height
和Width
为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 } }
交换两个循环。
for(int j=0; j
每个人都专注于宽度和高度,这不是解决方案。 GetPixel
有两个参数, x
和y
。 y
坐标必须是外部循环才能获得所需的顺序。
x坐标始终从0
... Width-1
翻转你的循环。 外环应该是高度,内环应该是宽度,如果你希望它像第一个图像一样。
只需交换宽度和高度:
for(int i=0;i
我还交换了i
和j
以便GetPixel
正常工作
让我们简单一点,使用x和y代替i和j,这样在笛卡尔坐标系中更容易思考。
//For each height, loop through all pixels at that height. for(int y=0; y < BMP.Height; y++) { for(int x=0; x < BMP.Width; x++) { color = BMP.GetPixel(x,y); } }