Reputation: 533
I am using WPF. A human web designer created a .xaml file containing several DrawingImage objects. This is used to display icons in the application. My question is, how can make this conversion to DrawingImage? I've tried using Inkscape but this creates a Canvas . I've also tried Blend, but this creates a DrawingBrush. Here is an example of one the designer created:
<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>
Upvotes: 0
Views: 1617
Reputation: 4005
I would recommend leaving it as a DrawingBrush
in your resources. But convert it to a DrawingImage
in your code behind.
var dBrush = Application.Current.Resources["key"] as DrawingBrush;
var dImage = new DrawingImage();
dImage.Drawing = dBrush.Drawing;
If you are going to be doing this a lot, then build a converter to avoid redundant code behind.
#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
Usage might be something like this:
<Image x:Name="ImageLogo" Source="{Binding Converter={StaticResource DrawingImageConverter}, ConverterParameter=images/logo.xaml}"/>
Upvotes: 1