Specifying SVG Images
- 2 minutes to read
This topic describes how to specify SVG Images:
In Markup
You can specify SVG images in markup using the SvgImageSourceExtension extension. This markup extension can be used with any property supporting the ImageSource value.
The following table shows the extension’s properties:
Property | Description |
---|---|
SvgImageSourceExtension.Uri | Gets or sets a System.Uri object that specifies the SVG image’s file. |
BaseSvgImageSourceExtension.Size | Gets or sets the SVG image’s sizes. |
BaseSvgImageSourceExtension.AutoSize | Gets or sets whether SVG image sizes are calculated automatically. |
The code sample below shows how to use the SvgImageSourceExtension to specify an SVG image as a BarButtonItem‘s glyph:
<dxb:BarContainerControl>
<dxb:MainMenuControl>
<dxb:BarButtonItem Glyph="{dx:SvgImageSource Uri=Images/Notebook1.svg}"/>
<dxb:BarButtonItem Glyph="{dx:SvgImageSource Uri=Images/Notebook2.svg}"/>
<dxb:BarButtonItem Glyph="{dx:SvgImageSource Uri=Images/Notebook3.svg}"/>
</dxb:MainMenuControl>
</dxb:BarContainerControl>
The following image shows the result:
Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T407507.
In Code-Behind
You can specify SVG images in code-behind. The code sample below shows a ViewModel with string paths to *.svg files:
public MainWindow() {
InitializeComponent();
DataContext = new List<String>() { "Images/First.svg", "Images/Last.svg" };
}
You can convert these string paths to ImageSource objects:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var uri = uriConverter.ConvertFrom(value) as Uri;
if (uri == null) return null;
var absoluteUri = uri.IsAbsoluteUri ? uri : new Uri(baseUri, uri);
using(var stream = UriStreamHelper.GetStream(absoluteUri)) {
object unused = null;
var image = SvgImageHelper.GetOrCreateSvgImage(stream, ref unused);
return WpfSvgRenderer.CreateImageSource(image, 1d, null, null, true);
}
}
The image below shows a ListBox with your SVG images:
Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T612359.