0

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: enter image description here

zetar
  • 1,225
  • 2
  • 20
  • 45
  • 1
    Welcome to invalid regions and delayed painting. When performing animation, you often have to disable double-buffering or force immediately update of the display. – Ben Voigt Jul 19 '13 at 14:11
  • If it is the case that the routine is very processor heavy consider multithreading some of the routines to expect better performance. – StevenTsooo Jul 19 '13 at 14:11
  • Welcome to while loops never mind delayed painting. **Edit** also, set pixel is horrible for most purposes when it comes to efficiency – Sayse Jul 19 '13 at 14:12
  • hmm if DrawDino2d works on some fields, it would be good practice to pass them as parameters to the method. Anyway, I think more peices of code are needed in order to understand your problem – br1 Jul 19 '13 at 14:12
  • How would I disable double-buffering or force an immediate update of the display? It's only in a while loop for testing purposes... eventually it's in an infinite loop until keypress (updating the position of the dinosaurs in the simulations). – zetar Jul 19 '13 at 14:16
  • "In a while loop I'm calling the following code 5 times". And there you are blocking the UI thread. Call that code in the `Tick` handler of a DispatcherTimer. – Clemens Jul 19 '13 at 15:31
  • I'm not finding any examples (except having to do with time) for using a 'Tick' handler of a DispatcherTimer. Can you point me in the right direction? – zetar Jul 19 '13 at 15:41

0 Answers0