Search code examples
c#silverlightwindows-phone-7live-tile

Render a "User Control" to an Image File


I followed the approach explained here to generate customized live tile appearance for my application.

The only problem this approach has is that you need to build the entire set of controls programmatically via c# code.

I tried adding a User Control to my application (LiveTile.xaml) and used the same approach by this time instead of adding all Rectangles and TextBlocks I instantiated the UserControl and rendered it on bitmap.

var tileUserControl = new MyLiveTile(); // this internally has a simple 50x50 red grid
bitmap.Render(tileUserControl, new TranslateTransform());

BUT: I no longer see anything rendered to the bitmap image. The image is entirely left black (even with very very simple xaml inside the user control).

Does anybody know how can this be achieved?

What I also tried

  • calling .UpdateLayout()
  • calling .Measure();
  • calling .Arrange();
  • calling .InvalidateMeasure();
  • calling .InvalidateArrange();

none seem to have an effect on the output.

Thanks.


Solution

  • Mind posting your entire rendering routine? You'll minimum have to call bitmap.Invalidate().

    Also, you might need to wait for the Control.Loaded event, to ensure that all the FrameworkElements on your UserControl have actually been loaded and rendered.

    Rendering the UserControl to early, will just render the UserControl element itself, without any of it's children (ie. a black square)