How To: Draw and Use SVG Images
- 6 minutes to read
Scalable Vector Graphics (SVG) is an XML-based vector image format. This article contains guidelines for creating SVG images and utilizing them within your applications.
Draw SVG Icons
Software
To draw SVG icons, launch the SVG Icon Builder tool available from Visual Studio’s “DEVEXPRESS” menu. You can also use third-party vector graphics editors, such as Adobe Illustrator, Inkscape, Adobe Flash Professional or CorelDRAW.
Image Size and Grid Settings
When creating SVG icons you should consider their intended application. For example, if you draw a 32x32 icon and have a 3-pixel wide shape, downscaling it to 16x16 pixels produces a 1.5 pixel line that looks blurry. To avoid this, make sure you use even dimensions. The figure below illustrates an image with 2-pixel grid cells.
DevExpress controls choose SVG icon colors according to the current application skin and\or SVG skin palette. Below is a button with a vector image as it appears in four different skins.
If you want your custom SVG icons to support this feature, either paint icon shapes with the same colors as DevExpress skins or utilize valid .css style names. The table below shows the six default DevExpress skin palette colors and their corresponding color codes:
Style Name | Color Hex Code | Color Sample |
---|---|---|
Red | #D04D2F | |
Green | #4DAE89 | |
Blue | #377AB5 | |
Yellow | #EEB764 | |
Black | #000000 | |
White | #FFFFFF |
Below is an XML markup for an SVG image created in Adobe Illustrator. In this markup, shapes are painted with the “White” and “Green” styles. Both names match those in the default DevExpress SVG palette. As a result, such an image can be repainted.
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 32 32" style="enable-background:new 0 0 32 32;" xml:space="preserve">
<style type="text/css">
.White{fill:#FFFFFF;}
.Green{fill:#039C23;}
</style>
<g id="Add">
<circle class="Green" cx="16" cy="16" r="14"/>
<polygon class="White" points="24,14 18,14 18,8 14,8 14,14 8,14 8,18 14,18 14,24 18,24 18,18 24,18"/>
</g>
</svg>
Restrictions
Gradient colors, animations, and external .css styles are not currently supported.
SVG Icon Builder
The SVG Icon Builder is a tool for creating vector icons by combining and re-painting sample images from DevExpress.
To launch the SVG Icon Builder:
- Run Visual Studio.
- Select DevExpress | All Platforms | Run SVG Icon Builder …
Alternatively, you can download the Icon Builder from the Microsoft Store as a stand-alone application.
In the following figure, a large (main) “Document” image and one small (action) “Pencil” image are combined to create a custom “Edit Document” vector icon:
To create icons in the SVG Icon Builder, drag images from the list onto the preview area. You can then use the dialog’s toolbox to move, rotate and flip images horizontally.
The color selector allows you to re-paint each part of a vector image.
Assign SVG Icons to DevExpress Controls
To assign a vector image to an item, expand the ImageOptions properties section and utilize the required …SvgImage properties. For example, BarItems provide the SvgImage and DisabledSvgImage properties for assigning vector icons for normal and disabled item states. Click the ellipsis button to invoke the DevExpress Image Selector dialog and choose a vector image from local storage or form/project resources.
You can also switch to the “DX Image Gallery” tab to choose vector icons from the DevExpress Image Gallery.
Every …SvgImage property has a related …SvgImageSize property. The figure below illustrates the BarItem class’s size properties.
Size properties behave in the following manner:
- If items provide properties that manage icon sizes, the …SvgImageSize settings are ignored. For example, the BarItem.RibbonStyle property specifies that a button icon is either 16x16 or 32x32 when this button is in a Ribbon.
If items use no specific logic to set their icon sizes, the …SvgImageSize properties are in effect. Set the icon size manually or leave it as 0x0 to draw the icon as is. For example, the Bar Manager does not limit icon sizes, so you can have 64x64 icons as shown below.
Assign SVG Icons Manually
If a control supports SVG Icons and has a related SvgImage property, create a new DevExpress.Utils.Svg.SvgImage class instance and assign it to that property. The SvgImage class provides the FromFile, FromResources and FromStream methods to load vector icons from different sources.
//load from file
SvgImage image = SvgImage.FromFile(@"D:\Work\Images\_SVG\Driving.svg");
//load from Resources
//image file's "Build Action" must be set to "Embedded Resource"
SvgImage image = SvgImage.FromResources("DXApplication1.Resources.Driving.svg", typeof(Form2).Assembly);
simpleButton1.ImageOptions.SvgImage = image;
The DevExpress.Utils library provides a public API for manually assigning SVG icons to third-party controls or DevExpress controls that do not support vector icons yet.
- Call the DevExpress.Utils.Svg.SvgBitmap.FromFile or DevExpress.Utils.Svg.SvgBitmap.FromStream method to initialize an SVG bitmap from a file or stream.
- Call the static DevExpress.Utils.Svg.SvgPaletteHelper.GetSvgPalette method to retrieve the current application skin‘s and visual element state’s color palette. Skip this step when assigning SVG icons to third-party controls that do not support DevExpress skins.
- Call the DevExpress.Utils.Svg.SvgBitmap.Render method to produce a raster image based on your SVG bitmap. This method takes two parameters: a palette (see the previous step) and a scale factor (set to 1 to draw an image as is). If you do not specify a scale factor manually, the icon renders according to the current system DPI setting.
The code below assigns the same vector icon to a Data Grid column and a standard WinForms button. The button receives a permanent icon that is never redrawn. The grid column, in turn, supports Glyph Skinning and you should render its icon accordingly to current Look And Feel and Skinning settings.
using DevExpress.Utils.Drawing;
using DevExpress.Utils.Svg;
namespace MyApp {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
// An SVG bitmap
SvgBitmap btm;
public Form1() {
InitializeComponent();
// Load a vector icon from file
btm = SvgBitmap.FromFile(@"D:\Work\Images\_SVG\Driving.svg");
// Produce and assign a raster image
// This image is 1.3 times larger than the original vector icon
button1.Image = btm.Render(null, 1.3);
// Assign a grid column image dynamically
gridView1.CustomDrawCell += GridView1_CustomDrawCell;
}
private void GridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) {
// Retrieve a color palette
var palette = SvgPaletteHelper.GetSvgPalette(this.LookAndFeel, ObjectState.Normal);
// Produce and draw a raster image
// The image size matches the current system DPI value
e.Cache.DrawImage(btm.Render(palette), Point.Empty);
}
}
}