代码之家  ›  专栏  ›  技术社区  ›  Sorskoot

使用远程图像渲染XAML

  •  2
  • Sorskoot  · 技术社区  · 14 年前

    我正在开发一个工具,用于将XAML片段呈现给图像。图像使用XAML作为模板来设计图像。由于渲染的工作方式,不可能使用代码隐藏。只渲染xaml是没有问题的。

    在我的一个模板中,我想给渲染器一个lat/long,并包括一个来自google地图的图像,以及其他存储在web上的图像。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>
    

    string.Format 将URL添加到模板。

    1 回复  |  直到 14 年前
        1
  •  0
  •   Sorskoot    14 年前

    我想出了一个解决办法。由于本地图像在渲染中起作用,我决定使用临时图像。在渲染XAML之前,我下载了图像,将其保存到磁盘并使用该路径作为图像源。

    此方法类似于:

    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;
        }
    }