我正在使用WPF。人类Web设计人员创建了一个包含多个DrawingImage对象的.xaml文件。这用于在应用程序中显示图标。我的问题是,如何将此转换为DrawingImage?我尝试过使用Inkscape,但这会创建一个Canvas。我也试过Blend,但这会创建一个DrawingBrush。以下是设计师创建的示例:
[i, j]
答案 0 :(得分: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
用法可能是这样的:
<Image x:Name="ImageLogo" Source="{Binding Converter={StaticResource DrawingImageConverter}, ConverterParameter=images/logo.xaml}"/>