I've got a an image (inside of a scroll viewer) that is 2,000 x 2,000 pixels. In a while loop I'm calling the following code 5 times:
writeableBitmap = new WriteableBitmap(CleanVegMap);
image.Source = writeableBitmap;
DrawDinos2d();
What is happening is that the image 'freezes' for about 10 seconds and then redraws the fifth, and final image. The feeling is that the system can't handle it.
If I just draw each image outside of the while loop from a pull-down menu they update properly.
The DrawDino2d()
method draws about 48 pixels on each bitmap. Just for clarification, the DrawDino2d()
is below:
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);
}
Here's an image of (extreme detail) of the 'dinos' being updated on the map: