增編:看來當我取消「優化代碼」正常運行,導致我相信這是一些古怪的配置問題C#試圖讀取或寫入受保護的內存錯誤
首先我試圖運行非託管代碼。我有「允許不安全的代碼」檢查。它指向這裏的代碼行,我試圖讀取一個位圖而不使用相對較慢的getpixel:
byte[] buff = { scanline[xo], scanline[xo + 1], scanline[xo + 2], 0xff };
整個片段在下面。我該如何糾正這個問題?
private const int PIXELSIZE = 4; // Number of bytes in a pixel
BitmapData mainImageData = mainImage.LockBits(new Rectangle(0, 0, mainImage.Width, mainImage.Height), ImageLockMode.ReadOnly, mainImage.PixelFormat);
List<Point> results = new List<Point>();
foundRects = new List<Rectangle>();
for (int y = 0; y < mainImageData.Height
{
byte* scanline = (byte*)mainImageData.Scan0 + (y * mainImageData.Stride);
for (int x = 0; x < mainImageData.Width; x++)
{
int xo = x * PIXELSIZE;
byte[] buff = { scanline[xo], scanline[xo + 1],
scanline[xo + 2], 0xff };
int val = BitConverter.ToInt32(buff, 0);
// Pixle value from subimage in desktop image
if (pixels.ContainsKey(val) && NotFound(x, y))
{
Point loc = (Point)pixels[val];
int sx = x - loc.X;
int sy = y - loc.Y;
// Subimage occurs in desktop image
if (ImageThere(mainImageData, subImage, sx, sy))
{
Point p = new Point(x - loc.X, y - loc.Y);
results.Add(p);
foundRects.Add(new Rectangle(x, y, subImage.Width,
subImage.Height));
}
}
}
PIXELSIZE的價值是什麼? – 2010-02-10 00:53:56
你確定圖像的形式是32bppRGB嗎?您不會在代碼中的任何位置指定或檢查像素格式。 – 2010-02-10 01:00:28
此外,只需在循環之前將指針設置爲Scan0一次,然後通過PIXELSIZE – 2010-02-10 01:02:34