In This Topic
Drawing engine for drawing of 2D graphics
VintaSoft Imaging .NET SDK provides
DrawingEngine class that defines interface for drawing of 2D graphics:
- draw/render image
- draw/fill/blend graphic path
- draw polygon/lines
- draw/fill rectangle/ellipse/pie/arc
- draw/measure text
- clip the drawing region
- apply transformation to the drawing engine
Also VintaSoft Imaging .NET SDK provides ready-to-use implementations of drawing engine:
- SkiaSharpDrawingEngine - cross-platform raster drawing engine, which is based on SkiaSharp library and allows to draw 2D graphics on raster image in Windows, Linux, macOS.
- GdiGraphics - raster drawing engine, which is based on System.Drawing.Common library and allows to draw 2D graphics on raster image in Windows only.
- PdfDrawingEngine - vector PDF drawing engine that allows to draw 2D graphics on PDF page (this drawing engine uses drawing engine SkiaSharpDrawingEngine or GdiGraphics).
- SvgDrawingEngine - vector drawing engine that allows to draw 2D graphics on SVG image (this drawing engine uses drawing engine SkiaSharpDrawingEngine or GdiGraphics).
- CompositeDrawingEngine - drawing engine, which combines behaviour of two drawing engines.
The licensed version of VintaSoft Imaging .NET SDK allows to create custom drawing engine or override behaviour of existing drawing engine. The evaluation version of SDK does not allow to create custom drawing engine or override behaviour of existing drawing engine.
Here is C#/VB.NET code that shows how to draw 2D graphics and text on PDF page:
using System.Drawing;
using Vintasoft.Imaging;
using Vintasoft.Imaging.Drawing;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Drawing;
using Vintasoft.Imaging.Pdf.Tree;
namespace CSHARP
{
class PdfDrawingEngineExample
{
/// <summary>
/// Creates PDF drawing engine on specified PDF page and performs drawing.
/// </summary>
/// <param name="width">PDF page width.</param>
/// <param name="height">PDF page height.</param>
/// <param name="resultImagePath">A path to save the drawing result.</param>
public static void DrawWithPdfDrawingEngine(int width, int height, string resultImagePath)
{
// create empty PDF document
using (PdfDocument document = new PdfDocument())
{
// page size
RectangleF pageSize = new RectangleF(0, 0, width, height);
// create new page of specified size
PdfPage page = new PdfPage(document, pageSize);
// add page to a PDF document
document.Pages.Add(page);
// create graphics from PDF page
using (PdfGraphics graphics = PdfGraphics.FromPage(page))
// create drawing engine
using (PdfDrawingEngine drawingEngine = new PdfDrawingEngine(graphics, true))
{
// create drawing area
RectangleF drawingArea = new RectangleF(0, 0, width, height);
// draw objects
DrawingExample(drawingEngine, drawingArea);
}
// save document
document.Save(resultImagePath);
}
}
/// <summary>
/// Draws graphic objects using specified drawing engine.
/// </summary>
/// <param name="drawingEngine">Drawing engine.</param>
/// <param name="drawingArea">Image area to draw objects in.</param>
public static void DrawingExample(DrawingEngine drawingEngine, RectangleF drawingArea)
{
// create bounding rectangle for ellipse
RectangleF ellipseRect = new RectangleF(
drawingArea.X + drawingArea.Width * 0.3f,
drawingArea.Y + drawingArea.Height * 0.3f,
drawingArea.Width * 0.4f,
drawingArea.Height * 0.4f);
// create brush and pen to draw ellipse
using (IDrawingSolidBrush brush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Blue))
{
using (IDrawingPen pen = drawingEngine.DrawingFactory.CreatePen(Color.Red, 4f))
{
// fill ellipse
drawingEngine.FillEllipse(brush, ellipseRect);
// stroke ellipse
drawingEngine.DrawEllipse(pen, ellipseRect);
}
}
// create brush and font to draw text
using (IDrawingSolidBrush textBrush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Yellow))
{
using (IDrawingFont font = drawingEngine.DrawingFactory.CreateFont("Arial", 30, true, false))
{
// create properties to layout text
TextLayoutProperties layoutProperties = new TextLayoutProperties(AnchorType.Center);
// draw text
drawingEngine.DrawText("Text", font, textBrush, ellipseRect, layoutProperties);
}
}
}
}
}
Imports System.Drawing
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Drawing
Imports Vintasoft.Imaging.Pdf
Imports Vintasoft.Imaging.Pdf.Drawing
Imports Vintasoft.Imaging.Pdf.Tree
Class PdfDrawingEngineExample
''' <summary>
''' Creates PDF drawing engine on specified PDF page and performs drawing.
''' </summary>
''' <param name="width">PDF page width.</param>
''' <param name="height">PDF page height.</param>
''' <param name="resultImagePath">A path to save the drawing result.</param>
Public Shared Sub DrawWithPdfDrawingEngine(width As Integer, height As Integer, resultImagePath As String)
' create empty PDF document
Using document As New PdfDocument()
' page size
Dim pageSize As New RectangleF(0, 0, width, height)
' create new page of specified size
Dim page As New PdfPage(document, pageSize)
' add page to a PDF document
document.Pages.Add(page)
' create graphics from PDF page
Using graphics As PdfGraphics = PdfGraphics.FromPage(page)
' create drawing engine
Using drawingEngine As New PdfDrawingEngine(graphics, True)
' create drawing area
Dim drawingArea As New RectangleF(0, 0, width, height)
' draw objects
DrawingExample(drawingEngine, drawingArea)
End Using
End Using
' save document
document.Save(resultImagePath)
End Using
End Sub
''' <summary>
''' Draws graphic objects using specified drawing engine.
''' </summary>
''' <param name="drawingEngine">Drawing engine.</param>
''' <param name="drawingArea">Image area to draw objects in.</param>
Public Shared Sub DrawingExample(drawingEngine As DrawingEngine, drawingArea As RectangleF)
' create bounding rectangle for ellipse
Dim ellipseRect As New RectangleF(drawingArea.X + drawingArea.Width * 0.3F, drawingArea.Y + drawingArea.Height * 0.3F, drawingArea.Width * 0.4F, drawingArea.Height * 0.4F)
' create brush and pen to draw ellipse
Using brush As IDrawingSolidBrush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Blue)
Using pen As IDrawingPen = drawingEngine.DrawingFactory.CreatePen(Color.Red, 4F)
' fill ellipse
drawingEngine.FillEllipse(brush, ellipseRect)
' stroke ellipse
drawingEngine.DrawEllipse(pen, ellipseRect)
End Using
End Using
' create brush and font to draw text
Using textBrush As IDrawingSolidBrush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Yellow)
Using font As IDrawingFont = drawingEngine.DrawingFactory.CreateFont("Arial", 30, True, False)
' create properties to layout text
Dim layoutProperties As New TextLayoutProperties(AnchorType.Center)
' draw text
drawingEngine.DrawText("Text", font, textBrush, ellipseRect, layoutProperties)
End Using
End Using
End Sub
End Class
Here is C#/VB.NET code that shows how to draw 2D graphics and text on SVG page:
using System.Drawing;
using System.Xml;
using Vintasoft.Imaging;
using Vintasoft.Imaging.Drawing;
using Vintasoft.Imaging.Drawing.Svg;
namespace CSHARP
{
class SvgDrawingEngineExample
{
/// <summary>
/// Creates SVG drawing engine on specified image and performs drawing.
/// </summary>
/// <param name="width">SVG image width.</param>
/// <param name="height">SVG image height.</param>
/// <param name="resultImagePath">A path to save the result SVG image.</param>
public static void DrawWithSvgDrawingEngine(int width, int height, string resultImagePath)
{
// create drawing engine
using (SvgDrawingEngine drawingEngine = new SvgDrawingEngine(width, height))
{
// create drawing area
RectangleF drawingArea = new RectangleF(0, 0, width, height);
// draw objects
DrawingExample(drawingEngine, drawingArea);
// create XML writer
using (XmlWriter xmlWriter = XmlWriter.Create(resultImagePath))
{
// get SVG content
SvgContent content = drawingEngine.GetContent();
// save SVG image
content.Save(xmlWriter);
}
}
}
/// <summary>
/// Draws graphic objects using specified drawing engine.
/// </summary>
/// <param name="drawingEngine">Drawing engine.</param>
/// <param name="drawingArea">Image area to draw objects in.</param>
public static void DrawingExample(DrawingEngine drawingEngine, RectangleF drawingArea)
{
// create bounding rectangle for ellipse
RectangleF ellipseRect = new RectangleF(
drawingArea.X + drawingArea.Width * 0.3f,
drawingArea.Y + drawingArea.Height * 0.3f,
drawingArea.Width * 0.4f,
drawingArea.Height * 0.4f);
// create brush and pen to draw ellipse
using (IDrawingSolidBrush brush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Blue))
{
using (IDrawingPen pen = drawingEngine.DrawingFactory.CreatePen(Color.Red, 4f))
{
// fill ellipse
drawingEngine.FillEllipse(brush, ellipseRect);
// stroke ellipse
drawingEngine.DrawEllipse(pen, ellipseRect);
}
}
// create brush and font to draw text
using (IDrawingSolidBrush textBrush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Yellow))
{
using (IDrawingFont font = drawingEngine.DrawingFactory.CreateFont("Arial", 30, true, false))
{
// create properties to layout text
TextLayoutProperties layoutProperties = new TextLayoutProperties(AnchorType.Center);
// draw text
drawingEngine.DrawText("Text", font, textBrush, ellipseRect, layoutProperties);
}
}
}
}
}
Imports System.Drawing
Imports System.Xml
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Drawing
Imports Vintasoft.Imaging.Drawing.Svg
Class SvgDrawingEngineExample
''' <summary>
''' Creates SVG drawing engine on specified image and performs drawing.
''' </summary>
''' <param name="width">SVG image width.</param>
''' <param name="height">SVG image height.</param>
''' <param name="resultImagePath">A path to save the result SVG image.</param>
Public Shared Sub DrawWithSvgDrawingEngine(width As Integer, height As Integer, resultImagePath As String)
' create drawing engine
Using drawingEngine As New SvgDrawingEngine(width, height)
' create drawing area
Dim drawingArea As New RectangleF(0, 0, width, height)
' draw objects
DrawingExample(drawingEngine, drawingArea)
' create XML writer
Using xmlWriter__1 As XmlWriter = XmlWriter.Create(resultImagePath)
' get SVG content
Dim content As SvgContent = drawingEngine.GetContent()
' save SVG image
content.Save(xmlWriter__1)
End Using
End Using
End Sub
''' <summary>
''' Draws graphic objects using specified drawing engine.
''' </summary>
''' <param name="drawingEngine">Drawing engine.</param>
''' <param name="drawingArea">Image area to draw objects in.</param>
Public Shared Sub DrawingExample(drawingEngine As DrawingEngine, drawingArea As RectangleF)
' create bounding rectangle for ellipse
Dim ellipseRect As New RectangleF(drawingArea.X + drawingArea.Width * 0.3F, drawingArea.Y + drawingArea.Height * 0.3F, drawingArea.Width * 0.4F, drawingArea.Height * 0.4F)
' create brush and pen to draw ellipse
Using brush As IDrawingSolidBrush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Blue)
Using pen As IDrawingPen = drawingEngine.DrawingFactory.CreatePen(Color.Red, 4F)
' fill ellipse
drawingEngine.FillEllipse(brush, ellipseRect)
' stroke ellipse
drawingEngine.DrawEllipse(pen, ellipseRect)
End Using
End Using
' create brush and font to draw text
Using textBrush As IDrawingSolidBrush = drawingEngine.DrawingFactory.CreateSolidBrush(Color.Yellow)
Using font As IDrawingFont = drawingEngine.DrawingFactory.CreateFont("Arial", 30, True, False)
' create properties to layout text
Dim layoutProperties As New TextLayoutProperties(AnchorType.Center)
' draw text
drawingEngine.DrawText("Text", font, textBrush, ellipseRect, layoutProperties)
End Using
End Using
End Sub
End Class
Drawing factory
VintaSoft Imaging .NET SDK provides
DrawingFactory class that defines interface for creating of drawing primitives (pen, brush, font, bitmap, etc) for drawing engine:
- create pen
- create solid brush, image brush, linear gradient brush, hatch brush
- create graphics path
- create region
- create bitmap
- create font, create fallback font
- create line cap
Also VintaSoft Imaging .NET SDK provides ready-to-use implementations of drawing factory:
- SkiaSharpDrawingFactory - the drawing factory for SkiaSharpDrawingEngine drawing engine. This factory is used as the default drawing factory if application is executed in Linux or macOS.
- GdiGraphicsFactory - the drawing factory for GdiGraphics drawing engine. This factory is used as the default drawing factory if application is executed in Windows.
The
DrawingFactory.Default property allows to get or set the default drawing factory. For example, it is possible to set
SkiaSharpDrawingFactory as the default drawing factory for Windows.
Here is C#/VB.NET code that shows how to set the default drawing factory:
/// <summary>
/// Sets the SKIA Sharp drawing factory as a default drawing factory.
/// </summary>
public static void SetSkiaSharpSdkDrawing()
{
// Skia - cross platform drawing engine
Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault();
}
/// <summary>
/// Sets the GDI drawing factory as a default drawing factory.
/// </summary>
public static void SetGdiSdkDrawing()
{
// GDI - Windows graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault();
}
''' <summary>
''' Sets the SKIA Sharp drawing factory as a default drawing factory.
''' </summary>
Public Shared Sub SetSkiaSharpSdkDrawing()
' Skia - cross platform drawing engine
Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault()
End Sub
''' <summary>
''' Sets the GDI drawing factory as a default drawing factory.
''' </summary>
Public Shared Sub SetGdiSdkDrawing()
' GDI - Windows graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault()
End Sub