I'm working on a tool that renders pieces of XAML to images.The XAML is used as a template to design the images. Due to the way the rendering works it is not possible to use a codebehind. Rendering just xaml is no problem.
In one of my templates I want to give the renderer a lat/long and include an image from google maps, among other images stored on the web. The XAML is rendered, but the images are not included. I assume this has to do something with the delay of downloading the images.
A template would look something like:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border CornerRadius="10" Background ="#FF123456" >
<Image Source="{0}" Width="250" Height="15开发者_JS百科0"/>
</Border>
</UserControl>
I use a string.Format
to add the URL to the template.
Does anyone know how I can render XAML with remote images?
I've come up with a workaround. Since local images do work in the rendering I decided to use temporary images. Before rendering the piece of XAML I download the image, save it to disk and use that path as the source of the image.
This method looks something like:
public string GetGoogleMapsImage(string lat, string lng, string path)
{
string googleMapsImage =
string.Format(
"http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom=8&size=250x150&sensor=false" , lat, lng);
string returnpath;
using (var w = new WebClient())
{
var gm = w.DownloadData(googleMapsImage);
returnpath = path + "\\~googletemp" + DateTime.Now.Ticks + ".png";
File.WriteAllBytes(returnpath, gm);
return returnpath;
}
}
精彩评论