Skip to content

Instantly share code, notes, and snippets.

@ScottIsAFool
Created May 19, 2014 20:29
Show Gist options
  • Save ScottIsAFool/e474b8150dd9732ee348 to your computer and use it in GitHub Desktop.
Save ScottIsAFool/e474b8150dd9732ee348 to your computer and use it in GitHub Desktop.
Save UserControl for tile
private async Task ToImage(UIElement element, string filename, double height, double width)
{
element.UpdateLayout();
element.Measure(new Size(width, height));
element.UpdateLayout();
element.Arrange(new Rect { Height = height, Width = width });
var bitmap = new WriteableBitmap((int)width, (int)height);
bitmap.Render(element, null);
bitmap.Invalidate();
await SaveTheImage(bitmap, filename);
GC.Collect();
}
private async Task SaveTheImage(WriteableBitmap bitmap, string filename)
{
using (var fileStream = await StorageService.CreateFileAsync(filename))
{
bitmap.SavePng(fileStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment