Wpf 使用远程图像渲染XAML
我正在开发一个将XAML片段渲染成图像的工具。XAML被用作设计图像的模板。由于渲染的工作方式,不可能使用代码隐藏。仅渲染xaml没有问题 在我的一个模板中,我想给渲染器一个lat/long,并包括一个来自googlemaps的图像,以及存储在web上的其他图像。将渲染XAML,但不包括图像。我想这与下载图像的延迟有关 模板的外观类似于:Wpf 使用远程图像渲染XAML,wpf,xaml,Wpf,Xaml,我正在开发一个将XAML片段渲染成图像的工具。XAML被用作设计图像的模板。由于渲染的工作方式,不可能使用代码隐藏。仅渲染xaml没有问题 在我的一个模板中,我想给渲染器一个lat/long,并包括一个来自googlemaps的图像,以及存储在web上的其他图像。将渲染XAML,但不包括图像。我想这与下载图像的延迟有关 模板的外观类似于: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta
<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="150"/>
</Border>
</UserControl>
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;
}
}
我使用string.Format
将URL添加到模板中
有人知道如何使用远程图像渲染XAML吗?我想出了一个解决方法。由于本地图像在渲染中起作用,我决定使用临时图像。在渲染XAML片段之前,我下载图像,将其保存到磁盘,并使用该路径作为图像源 此方法类似于:
<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="150"/>
</Border>
</UserControl>
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;
}
}
我想出了一个解决办法。由于本地图像在渲染中起作用,我决定使用临时图像。在渲染XAML片段之前,我下载图像,将其保存到磁盘,并使用该路径作为图像源 此方法类似于:
<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="150"/>
</Border>
</UserControl>
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;
}
}