我有这段代码:
private void FindPoints()
{
try
{
GraphicsPath gp = new GraphicsPath();
int x, y, p, j, wdthHght;
int bytes;
byte[] rgbValuesWithClouds;
byte[] rgbValuesWithoutClouds;
IntPtr ptr;
Rectangle rect;
BitmapData bitmap_Data;
Bitmap bmpWithClouds; //No memory is allocated
Bitmap bmpWithoutClouds; //No memory is allocated
gp.AddEllipse(new RectangleF(73, 72, 367, 367));
using (bmpWithClouds = new Bitmap(mymem))
{
rect = new Rectangle(0, 0, bmpWithClouds.Width, bmpWithClouds.Height);
wdthHght = bmpWithClouds.Width;
bitmap_Data = bmpWithClouds.LockBits(rect, ImageLockMode.ReadWrite, bmpWithClouds.PixelFormat);
ptr = bitmap_Data.Scan0;
bytes = bitmap_Data.Stride * bmpWithClouds.Height;
rgbValuesWithClouds = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValuesWithClouds, 0, bytes);
bmpWithClouds.UnlockBits(bitmap_Data);
}
su = System.IO.Directory.GetCurrentDirectory();
using (bmpWithoutClouds = new Bitmap(su + "\\WithoutClouds.bmp")) //24 bit bitmap
{
rect = new Rectangle(0, 0, bmpWithoutClouds.Width, bmpWithoutClouds.Height);
bitmap_Data = bmpWithoutClouds.LockBits(rect, ImageLockMode.ReadWrite, bmpWithoutClouds.PixelFormat);
ptr = bitmap_Data.Scan0;
bytes = bitmap_Data.Stride * bmpWithoutClouds.Height;
rgbValuesWithoutClouds = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValuesWithoutClouds, 0, bytes);
bmpWithoutClouds.UnlockBits(bitmap_Data);
}
cloudPoints = new List<Point>();
for (y = 0; y < wdthHght; y++)
{
j = 0;
for (x = 0; x < wdthHght; x++)
{
p = y * wdthHght * 3 + j;
if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])
{
cloudPoints.Add(new Point(x, y));
}
j += 3;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
例外是第393行:
if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])
我使用了try和catch但我无法看到p
以及rgbValuesWithClouds
和rgbValuesWithoutClouds
的值是什么。
还有什么可以使这个例外?
发现了System.IndexOutOfRangeException 的HResult = -2146233080 Message = Index超出了数组的范围。 来源=我的气象站 堆栈跟踪: 在mws.ScanningClouds.FindPoints()在d:\ C-Sharp \下载文件\下载 - 文件 - 项目 - 版本-012 \下载文件\ ScanningClouds.cs:第393行 InnerException:
答案 0 :(得分:1)
您已经知道
行中有IndexOutOfRangeException
if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])
表示p
小于0
或大于rgbValuesWithClouds
或rgbValuesWithoutClouds
的长度。
在进行此比较之前,您可能需要检查p
是否大于或等于0
且小于两个阵列的长度。