locked
Poor performance with WriteableBitmap RRS feed

  • Question

  • Hi,

    I want to create thousands of Line instances in Metro app but it takes lot of time if I use Path or Line objects. Instead I am using WriteableBitmap to generate fast images by drawing directly to bitmap. I am following the similar approach as shown here. But with WriteableBitmap, it is taking more time than shape elements like Path or Line.

    I followed the similar approach in Silverlight, WPF and Windows Phone, there WriteableBitmap was very fast. But in Metro it takes a very long time to create a bitmap of 200*200 size.

    I am creating instance for WriteableBitmap only once and writing pixel information inside it.

    Example:

    WriteableBitmap bitmap = new WriteableBitmap(200, 200);
    
                Windows.UI.Color color = Windows.UI.Colors.Red;
    
                var a = color.A + 1;
                var col = (color.A << 24)
                         | ((byte)((color.R * a) >> 8) << 16)
                         | ((byte)((color.G * a) >> 8) << 8)
                         | ((byte)((color.B * a) >> 8));
    
                Int32 index = 1;
                Random rand = new Random();
    
                int lastY = rand.Next(10, 50);
    
                for (Int32 i = 0; i < 500; i++)
                {
                    int newY = rand.Next(10, 50);
                    bitmap.DrawLine(index, lastY, index + 10, newY, col);
                    lastY = newY;
                    
                    index = index + 10;
                }
    
                Image img = new Image();
                img.Source = bitmap;
                LayoutRoot.Children.Add(img);
            }

    Is there any performance issue with WriteableBitmap in Metro?


    • Edited by JordanSmith Monday, August 20, 2012 10:27 AM edited code
    Monday, August 20, 2012 10:19 AM

Answers

  • It is resolved. I have to wrap my calls in a simple using(bitmap.GetBitmapContext()) as shown in the following link.

    http://kodierer.blogspot.de/2012/05/one-bitmap-to-rule-them-all.html

    • Marked as answer by JordanSmith Monday, August 20, 2012 12:58 PM
    Monday, August 20, 2012 12:58 PM