Skip to content

Instantly share code, notes, and snippets.

@ceee
Created May 19, 2014 20:22
Show Gist options
  • Save ceee/ec46d5bde94d7a72c189 to your computer and use it in GitHub Desktop.
Save ceee/ec46d5bde94d7a72c189 to your computer and use it in GitHub Desktop.
public static Uri SaveTile(UserControl control, string filename, double width, double height)
{
string path = tileFolder + filename + ".png";
bool success = true;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
control.Width = width;
control.Height = height;
control.Measure(new Size(width, height));
control.Arrange(new Rect(0, 0, width, height));
try
{
ExtendedImage tileImaged = control.ToImage();
if (tileImaged == null || tileImaged.PixelWidth < 100)
{
success = false;
}
else
{
Encoders.AddEncoder<PngEncoder>();
PngEncoder pngEncoder = new PngEncoder();
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = isf.OpenFile(path, System.IO.FileMode.OpenOrCreate))
{
pngEncoder.Encode(tileImaged, stream);
stream.Close();
success = true;
}
}
}
}
catch
{
success = false;
}
});
if (success == false)
{
throw new PokiException("Tile could not be saved");
}
return new Uri("isostore:" + path, UriKind.Absolute);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment