我有一个 2,000 x 2,000 像素的图像(在滚动查看器内)。在一个 while 循环中,我调用了以下代码 5 次:
writeableBitmap = new WriteableBitmap(CleanVegMap);
image.Source = writeableBitmap;
DrawDinos2d();
发生的事情是图像“冻结”大约 10 秒,然后重绘第五张也是最后一张图像。感觉是系统处理不了。
如果我只是从下拉菜单中在 while 循环之外绘制每个图像,它们会正确更新。
该DrawDino2d()
方法在每个位图上绘制大约 48 个像素。只是为了澄清,DrawDino2d()
下面是:
public void DrawDinos2d()
{
for (int i = 0; i < MainWindow.Dinosaurs.Count(); i++)
{
for (int j = 0; j < MainWindow.Dinosaurs[i].Location.Count; j++)
SetPixel(writeableBitmap, (int)MainWindow.Dinosaurs[i].Location[j].X, (int)MainWindow.Dinosaurs[i].Location[j].Y, System.Windows.Media.Color.FromArgb(127, 255, 255, 0));
}
}// DrawDinos2d
public void SetPixel(WriteableBitmap wb, int x, int y, System.Windows.Media.Color color)
{
var pixelRect = new Int32Rect(x, y, 1, 1);
var pixelBuffer = new byte[4];
wb.CopyPixels(pixelRect, pixelBuffer, 4, 0);
pixelBuffer[0] = (byte)(pixelBuffer[0] * (1F - color.ScA) + color.B * color.ScA);
pixelBuffer[1] = (byte)(pixelBuffer[1] * (1F - color.ScA) + color.G * color.ScA);
pixelBuffer[2] = (byte)(pixelBuffer[2] * (1F - color.ScA) + color.R * color.ScA);
wb.WritePixels(pixelRect, pixelBuffer, 4, 0);
}
这是地图上正在更新的“恐龙”的(极端细节)图像: