title: Custom tile generates a System.OutOfMemoryException exception in wp8
body: <p>Has anyone got any idea why I'm getting an OutOfMemoryException when I'm creating my custom tile?</p>
  
  <p>I'm trying to create custom images for my primary tile on a windows phone 8 app from a ScheduledAgent. The error doesn't occur until my very last line of code is executed which is the NotifyComplete().</p>
  
  <p>Here is the code (Not the cleanest but ok for prototyping I guess). This code only handles the wide tile and it tries to load an image an image downloaded from a website and then it tries to render a logo and a description over this image.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>    private void CreateTiles()
      {
          Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
          {
              for (int i = 0; i &lt; 2; i++)
              {
                  var bmp = new WriteableBitmap(691, 336);
                  var articleImg = new BitmapImage(new Uri(articles[i].ImageFilename, UriKind.Relative));
                  var articleImage = new Image { Source = articleImg };
                  articleImage.Stretch = Stretch.UniformToFill;
                  articleImg.CreateOptions = BitmapCreateOptions.None; // Force the bitmapimage to load it's properties so the transform will work
  
                  var bmpLogo = new WriteableBitmap(100, 100);
                  var logoImg = new BitmapImage(new Uri("/Assets/Tiles/FlipCycleTileSmall.png", UriKind.Relative));
                  var logoImage = new Image { Source = logoImg };
                  logoImage.Opacity = 1.0;
                  logoImg.CreateOptions = BitmapCreateOptions.None; // Force the bitmapimage to load it's properties so the transform will work
  
                  var articleBannerGrid = new Grid();
                  articleBannerGrid.Background = ColorExtensions.ToSolidColorBrush("#000F558E");
                  articleBannerGrid.Opacity = .5;
                  articleBannerGrid.Height = 100;
                  articleBannerGrid.VerticalAlignment = VerticalAlignment.Bottom;
                  articleBannerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
                  articleBannerGrid.ColumnDefinitions.Add(new ColumnDefinition());
                  articleBannerGrid.Children.Add(logoImage);
  
                  Grid.SetColumn(logoImage, 0);
  
                  var textBlock = new TextBlock();
                  textBlock.Text = articles[i].Description;
                  textBlock.FontWeight = FontWeights.Bold;
                  textBlock.Margin = new Thickness(10, 5, 30, 5);
                  textBlock.TextWrapping = TextWrapping.Wrap;
                  textBlock.Foreground = new SolidColorBrush(Colors.White); //color of the text on the Tile
                  textBlock.FontSize = 30;
                  textBlock.Opacity = 1.0;
  
                  var articleTextGrid = new Grid();
                  articleTextGrid.Height = 100;
                  articleTextGrid.VerticalAlignment = VerticalAlignment.Bottom;
                  articleTextGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
                  articleTextGrid.ColumnDefinitions.Add(new ColumnDefinition());
                  articleTextGrid.Children.Add(textBlock);
  
                  Grid.SetColumn(textBlock, 1);
  
                  var canvas = new Grid();
                  canvas.Width = articleImg.PixelWidth;
                  canvas.Height = articleImg.PixelHeight;
                  canvas.Children.Add(articleImage);
                  canvas.Children.Add(articleBannerGrid);
                  canvas.Children.Add(articleTextGrid);
  
                  bmp.Render(canvas, null);
                  bmp.Invalidate(); //Draw bitmap
  
                  FileStream fs = new FileStream(articles[i].ImageFilename, FileMode.Create);
                  bmp.SaveJpeg(fs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
                  fs.Close();
                  fs.Dispose();
                  articleImage = null;
                  articleImg = null;
              }
  
              //GC.Collect();
              //GC.WaitForPendingFinalizers();
  
              ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault();
  
              if (TileToFind != null)
              {
                  string title = articles[0].Tag;
                  string backTitle = articles[1].Tag;
                  string content = articles[0].Description;
                  string backContent = articles[1].Description;
  
                  FlipTileData tileData = new FlipTileData()
                  {
                      Title = title,
                      BackTitle = backTitle,
                      BackContent = backContent,
                      WideBackContent = backContent,
                      BackgroundImage = new Uri(articles[0].ImageFilename, UriKind.Relative),
                      BackBackgroundImage = new Uri(articles[1].ImageFilename, UriKind.Relative),
                      WideBackgroundImage = new Uri(articles[0].ImageFilename, UriKind.Relative),
                      WideBackBackgroundImage = new Uri(articles[1].ImageFilename, UriKind.Relative),
                  };
  
                  TileToFind.Update(tileData);
              }
  
              NotifyComplete();
  
          });
      }
  </code></pre>
  
  <p>I assume that it is the correct place to generate the custom tile i.e. ScheduledAgent.</p>
  
  <p>I found an article <a href="http://developer.nokia.com/community/discussion/showthread.php/239433-Dynamic-Live-Tile-issue-WP8-WriteableBitmap" rel="nofollow">Dynamic Live Tile issue WP8 [WriteableBitmap]</a> on the Nokia website with the same problem but no solution there either.</p>
  
  <p>I'll continue debugging this tomorrow by removing everything and adding each bit individually step by step to see if I can spot anything but if anyone has got any suggestion or solution, I'd appreciate if you could help.</p>
  
  <p>If I'm going about it the wrong way, please let me know what's the best method to update tiles in the scheduled agent.</p>
  
  <p>Thanks.</p>