Note that there are some explanatory texts on larger screens.

plurals
  1. POOutOfMemoryException on WP8 (C# & Silverlight) because of WritableBitmap
    primarykey
    data
    text
    <p>I'm creating and application that needs to add and remove a lot of <code>UIElement</code> to a <code>Canvas</code>. Basically a <code>Canvas</code> contains a collection of <code>UIElement</code> and automatically renders/updates it on the screen depending what it contains.</p> <p>In order to avoid having a tons of <code>UIElements</code> who overlap each others on the screen I prefer to add all of them on a secondary <code>Canvas</code> then create a <code>Image</code> from it (thanks to <code>WritableBitmap</code>). Finally I add this <code>Image</code> on my current <code>Canvas</code>. By allowing to have only few image on my Canvas I expect to have better performance. Unfortunately it seems I can't delete completely the <code>WritableBitmap</code>, even if I set it to <code>null</code>.</p> <p>The following code illustrates it :</p> <pre><code>//My constructor public WP8Graphics() { //Here my collection DataBinded with the Canvas from the Mainpage this.UIElements = new ObservableCollection&lt;UIElement&gt;(); //The secondary Canvas GraphicCanvas = new Canvas(); GraphicCanvas.Height = MainPage.CurrentCanvasHeight; GraphicCanvas.Width = MainPage.CurrentCanvasWidth; } ///This method can be hit thousand times, it basically create a rectangle public void fillRect(int x, int y, int width, int height) { // some code // CREATE THE RECTANGLE rect GraphicCanvas.Children.Add(rect); // My secondary Canvas WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null); wb1.Invalidate(); WriteableBitmap wb2 = new WriteableBitmap((int)MainPage.CurrentCanvasWidth, (int)MainPage.CurrentCanvasHeight); for (int i = 0; i &lt; wb2.Pixels.Length; i++) { wb2.Pixels[i] = wb1.Pixels[i]; } wb2.Invalidate(); wb1 = null; Image thumbnail = new Image(); thumbnail.Height = MainPage.CurrentCanvasHeight; thumbnail.Width = MainPage.CurrentCanvasWidth; thumbnail.Source = wb2; this.UIElements.Add(thumbnail); } </code></pre> <p>After something like 24 <code>WriteableBitmap</code> created a <strong>OutOfMemoryException</strong> appears. I read many articles about this problem and in my case it seems the <code>WriteableBitmap</code> depends on my <strong>GraphicCanvas</strong> and remains because there still have a reference to it. I can't delete my Graphic Canvas nor set <strong>myImage</strong> source to null.</p> <p>I have 2 question :</p> <ul> <li>Is there another way to create an image from Canvas or a collection of UIElements ?</li> <li>Is it possible to remove the reference who keeps that WriteableBitmap alive ?</li> </ul> <p>I hope to be enough clear and easy to read. </p> <p>Thank you for reading.</p> <p><strong>EDITED with atomaras suggestion, but still the same problem</strong></p> <p><code>WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null);</code> This line still throws OutOfMemoryException.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload