windowsforms-117671-controls-and-libraries-diagrams-diagram-items-create-custom-diagram-items-svg-shapes.md
The DiagramControl allows you to use SVG images as shapes. In this case, an SVG image describes shape geometry, but does not affect shape content and connection points.
Use the ShapeDescription.CreateSvgShape method to create a shape from a stream that contains an SVG image:
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<circle cx="50" cy="50" r="40" stroke-width="4" stroke="blue" fill="green"/>
</svg>
void RegisterStencil() {
var stencil = new DiagramStencil("CustomStencil", "SVG Shapes");
System.IO.FileStream stream = System.IO.File.Open("..\\..\\greencircle.svg", System.IO.FileMode.Open);
var shapeDescription = DevExpress.Diagram.Core.ShapeDescription.CreateSvgShape(
shapeId: "SVGCircle",
name: "SVG Circle",
svgStream: stream,
getConnectionPoints: (s) => new[] { new System.Windows.Point(s.Width / 2, s.Height / 2) }
);
stencil.RegisterShape(shapeDescription);
DiagramToolboxRegistrator.RegisterStencil(stencil);
}
Private Sub RegisterStencil()
Dim stencil = New DiagramStencil("CustomStencil", "SVG Shapes")
Dim stream As System.IO.FileStream = System.IO.File.Open("..\..\greencircle.svg", System.IO.FileMode.Open)
Dim shapeDescription = DevExpress.Diagram.Core.ShapeDescription.CreateSvgShape(
shapeId:="SVGCircle",
name:="SVG Circle",
svgStream:=stream,
getConnectionPoints:=Function(s) {New System.Windows.Point(s.Width / 2, s.Height / 2)}
)
stencil.RegisterShape(shapeDescription)
DiagramToolboxRegistrator.RegisterStencil(stencil)
End Sub
View Example: Register and Use SVG Shapes
The DiagramControl does not automatically recognize SVG image elements as background or stroke lines. As a result, it does not colorize SVG shapes when a user changes diagram themes, styles, or the DiagramItem.Appearance property.
The ShapeDescription.CreateSvgShape method has two optional parameters: backgroundColorCode and strokeColorCode. These parameters accept color codes from an SVG image’s markup and allow the DiagramControl to replace them once a user applies a theme/style.
The following SVG image has green background and blue stroke:
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<circle cx="50" cy="50" r="40" stroke-width="4" stroke="blue" fill="green"/>
</svg>
Pass image color codes to backgroundColorCode and strokeColorCode parameters:
var shapeDescription = DevExpress.Diagram.Core.ShapeDescription.CreateSvgShape(
shapeId: "SVGCircle",
name: "SVG Circle",
svgStream: stream,
getConnectionPoints: (s) => new[] { new System.Windows.Point(s.Width / 2, s.Height / 2) },
backgroundColorCode: "green",
strokeColorCode: "blue"
);
Dim shapeDescription = DevExpress.Diagram.Core.ShapeDescription.CreateSvgShape(
shapeId:="SVGCircle",
name:="SVG Circle",
svgStream:=stream,
getConnectionPoints:=Function(s) {New System.Windows.Point(s.Width / 2, s.Height / 2)},
backgroundColorCode:="green",
strokeColorCode:="blue"
)
The DiagramControl can use only one color code to identify background and stroke colors. Multiple color codes are not supported.
When you create diagram shapes from SVG images, you cannot control the initial shape size and other settings. To pre-define those attributes, create and register a FactoryItemTool. For basic information about that class, review the first example in the overview topic: Create Custom Diagram Items.
Note that you must register both the shape class and the tool within the diagram. That means you will have two separate items in the Shapes Panel. If users do not need to access the original shape item without pre-defined settings, register that shape in an invisible stencil. The example below shows how to do this.
void CreateToolboxItems() {
System.IO.FileStream stream = System.IO.File.Open("..\\..\\greencircle.svg", System.IO.FileMode.Open);
// Visible stencil:
var stencil = new DevExpress.Diagram.Core.DiagramStencil("svgStencil", "SVG Shapes");
// Invisible stencil:
var stencilContainer = new DevExpress.Diagram.Core.DiagramStencil("container", "container", false);
// Create an SVG Shape and register it in the invisible stencil:
stencilContainer.RegisterShape(ShapeDescription.CreateSvgShape("SVGCircle", "SVG Circle", stream));
// Create a FactoryItemTool that specifies SVG shape properties:
var tool = new FactoryItemTool(
"svgTool",
() => "SVG Circle",
diagram => {
stream.Position = 0;
var svgShape = new DiagramShape() {
Shape = stencilContainer.GetShape("SVGCircle")
};
return svgShape;
},
new System.Windows.Size(50, 50),
true
);
// Register this tool in the visible stencil:
stencil.RegisterTool(tool);
// Register invisible and visible stencils:
DevExpress.Diagram.Core.DiagramToolboxRegistrator.RegisterStencil(stencilContainer);
DevExpress.Diagram.Core.DiagramToolboxRegistrator.RegisterStencil(stencil);
diagramControl1.SelectedStencils.Add("svgStencil");
}
Private Sub CreateToolboxItems()
Dim stream As System.IO.FileStream = System.IO.File.Open("..\..\greencircle.svg", System.IO.FileMode.Open)
' Visible stencil:
Dim stencil = New DevExpress.Diagram.Core.DiagramStencil("svgStencil", "SVG Shapes")
' Invisible stencil:
Dim stencilContainer = New DevExpress.Diagram.Core.DiagramStencil("container", "container", False)
' Create an SVG Shape and register it in the invisible stencil:
stencilContainer.RegisterShape(ShapeDescription.CreateSvgShape("SVGCircle", "SVG Circle", stream))
' Create a FactoryItemTool that specifies SVG shape properties:
Dim tool = New FactoryItemTool(
"svgTool",
Function() "SVG Circle",
Function(diagram)
stream.Position = 0
Dim svgShape = New DiagramShape() With {
.Shape = stencilContainer.GetShape("SVGCircle")
}
Return svgShape
End Function,
New System.Windows.Size(50, 50),
True
)
' Register this tool in the visible stencil:
stencil.RegisterTool(tool)
'Register invisible and visible stencils:
DevExpress.Diagram.Core.DiagramToolboxRegistrator.RegisterStencil(stencilContainer)
DevExpress.Diagram.Core.DiagramToolboxRegistrator.RegisterStencil(stencil)
diagramControl1.SelectedStencils.Add("svgStencil")
End Sub
<path><g><use><rect><circle><ellipse><line><polyline><polygon><image><text><use><linearGradient><radialGradient>transform attributestroke-linecap attributestyle attributeopacity attributeSupported stroke and fill properties:
strokestroke-widthfillSVG shapes support only base SVG elements and features. The list of unsupported elements includes (but is not limited to) the following items:
enable-background parameterpattern elementdisplay attributehref attribute in gradientsclass and id attributesclipPath element can cause clipping in DirectX mode.