当访问最后一个像素 (x = 255, y = 255) 时,以下代码总是在 Fill 方法中导致 AccessViolationException。但是,如果我使用诸如 200x200 之类的尺寸,它就可以工作。512 x 512 或 1024 x 1024 或其他二次方尺寸(似乎适用于 4x4、8x8 ... 高达 32 x 32)也有同样的问题。任何想法为什么会这样?
var wb = new WriteableBitmap(256, 256, 96, 96, PixelFormats.Bgr24, null);
wb.Fill(256, 256, 3, Colors.Black);
...
public static void Fill(this WriteableBitmap bmp, int width, int height, int bytesPerPixel, Color color)
{
var rect = new Int32Rect(0, 0, width, height);
var fillColor = color.ToInt();
bmp.Lock();
unsafe
{
int pBackBuffer = (int)bmp.BackBuffer;
int stride = bmp.BackBufferStride;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
int offset = y * stride + x * bytesPerPixel;
int pBackBufferWithOffset = pBackBuffer + offset;
*((int*) pBackBufferWithOffset) = fillColor;
}
}
bmp.AddDirtyRect(rect);
bmp.Unlock();
}
private static int ToInt(this Color color)
{
int c = color.R << 16; // R
c |= color.G << 8; // G
c |= color.B << 0; // B
return c;
}