2015-08-14 209 views
0

我正在使用WPF。人類網頁設計師創建了一個包含多個DrawingImage對象的.xaml文件。這用於在應用程序中顯示圖標。我的問題是,如何使這個轉換爲DrawingImage?我試過使用Inkscape,但是這會創建一個Canvas。我也嘗試過Blend,但是這會創建一個DrawingBrush。下面是創建了一個設計師的例子:將圖像轉換爲DrawingImage

<DrawingImage x:Key='example'> 
    <DrawingImage.Drawing> 
     <DrawingGroup> 
      <DrawingGroup.Children> 
       <GeometryDrawing Brush="#FF000000" Geometry="M165.357,46.929L50.644,46.929C49.231,46.929 48.009,47.445 46.977,48.477 45.945,49.509 45.429,50.731 45.429,52.143L45.429,130.357C45.429,131.769 45.945,132.992 46.977,134.023 48.009,135.055 49.231,135.572 50.644,135.572L165.357,135.572C166.769,135.572 167.991,135.055 169.023,134.023 170.054,132.992 170.57,131.769 170.57,130.357L170.57,52.143C170.57,73,118.428,73L97.571,73C96.159,73 94.937,72.484 93.905,71.452 92.873,70.42 92.357,69.198 92.357,67.785 92.357,66.373 92.873,65.151 93.905,64.119 94.937,63.087 96.159,62.571 97.571,62.571L118.43,62.571C119.841,62.571 121.063,63.087 122.096,64.119 123.127,65.151 123.643,66.373 123.643,67.785 123.643,69.198 123.126,70.42 122.094,71.452z" /> 
       <GeometryDrawing Brush="#FF000000" Geometry="M174.238,11.977C173.206,10.945,171.984,10.429,170.572,10.429L45.429,10.429C44.017,10.429 42.795,13.009 40.215,14.231 40.215,15.643L40.215,36.5C40.215,37.912 40.731,39.134 41.763,40.166 42.795,41.198 44.017,41.714 45.429,41.714L170.57,41.714C171.982,41.714 173.205,41.198 174.238,40.166 175.269,39.134 175.785,37.912 175.785,36.5L175.785,15.643C175.785,14.23,175.27,13.009,174.238,11.977z" /> 
      </DrawingGroup.Children> 
     </DrawingGroup> 
    </DrawingImage.Drawing> 
</DrawingImage> 
+0

轉換? – VMaleev

+0

任何東西基本上... JPG,PNG,矢量... – user2884789

+0

@ user2884789 - 您的問題需要一些改進。你提到哪個「設計師」工具?這聽起來像是你正在使用一些特定的工具來創建圖像並將其導出到XAML中,並且您正在使用的工具不會讓您選擇導出爲DrawingImage。 –

回答

1

我會建議把它當作你的資源DrawingBrush。但是在你的代碼中將它轉換爲DrawingImage

var dBrush = Application.Current.Resources["key"] as DrawingBrush; 
var dImage = new DrawingImage(); 
dImage.Drawing = dBrush.Drawing; 

如果你打算做很多事情,那麼建立一個轉換器來避免多餘的代碼。

#region class DrawingImageConverter 
/// <summary> 
/// Converter class to generate a DrawingImage from a URI, even if it points to a DrawingBrush. 
/// </summary> 
public class DrawingImageConverter : System.Windows.Data.IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string path = parameter.ToString(); 

     System.Windows.Resources.StreamResourceInfo sri = System.Windows.Application.GetResourceStream(new Uri(path, UriKind.Relative)); 
     if (sri != null) 
     { 
      using (System.IO.Stream stream = sri.Stream) 
      { 
       var logo = System.Windows.Markup.XamlReader.Load(stream); 

       if (null != (logo as System.Windows.Media.DrawingImage)) 
       { 
        return logo; 
       } 
       if (null != (logo as System.Windows.Media.DrawingBrush)) 
       { 
        var img = new System.Windows.Media.DrawingImage(); 
        img.Drawing = (logo as System.Windows.Media.DrawingBrush).Drawing; 
        return img; 
       } 

       throw new Exception("Resource unknown type : " + path); 
      } 
     } 

     throw new Exception("Resource not found : " + path); 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
#endregion class DrawingImageConverter 

用法可能是這樣的:從** **什麼來DrawingImage

<Image x:Name="ImageLogo" Source="{Binding Converter={StaticResource DrawingImageConverter}, ConverterParameter=images/logo.xaml}"/>