Monday, August 17, 2009

Snapshot of scrollable content using Silverlight 3 WriteableBitmap

Recently, I've been working on exporting part of XAML tree to Jpeg image using Silverlight 3 WriteableBitmap and it all went pretty well until I put lots of stuff into exporting content container. Here are some of the problems I've encountered:
  • Scrollbar appears in the snapshot.
  • Non-visible content doesn't appear in the snapshot even when its size increased.
Here is my approach for solving these problems:

1. Prior to creating WriteableBitmap adjust user interface:
  • Adjust the size (width and height) of scrolling content so that the scrollbar doesn't appear.
  • Adjust the size of content containers including plugin size (!), so that you don't have cut offs in exported image.
  • If there are some UIElements you don't want exported, set them as invisible.
2. Create WriteableBitmap.
3. Revert user interface changes.

The part of code that solves scrolling problems in my scenario is published here. Feel free to adjust it to your needs and let me know what you think. I wonder how this could be accomplished in Out Of Browser (OOP) scenario. How can the platform window be resized to get all the scrollable content without cut offs, and does the cut-off problem even exist? I might even try that one day ;).
Example of generating image from (scrollable) content is available online here, just choose Timetable > Export to Jpeg. That's it.


No comments:

Post a Comment