-
Notifications
You must be signed in to change notification settings - Fork 1
2.20 Windows Presentation Foundation WPF
Drawing WPF graphics is a four steps process.
- Create
DrawWPFPath
object. Input arguments are path and Y axis direction. - Optionally add a brush by calling one of seven
SetBrush
methods or callUseCurrentBrush
method. - Optionally add a pen by calling one of two
SetPen
methods or callUseCurrentPen
method. - Draw the graphics object by calling
PdfContents.DrawWPFPath
.
If System.Windows.Media
reference is not available (i.e. your application is Windows Form), you need to add PresentationCore
and WindowsBase
assemblies to your application.
Programming example of drawing with WPF graphics classes is given in OtherExample.cs Example8e and Example8f.
If no brush and no pen are defined the draw graphics is taken as a clip path.
Below you will find more details how to use the DrawWPFPath
class for both WPF applications and other applications.
DrawWPFPath
constructor takes two arguments: path and Y axis direction. Path is either a string or System.Windows.Media.PathGeometry
. The text string is defined in Path Markup Syntax. The path geometry class is described in PathGeometry Class. The Y axis direction is down when the path is defined for WPF environment. The Y axis is up for PDF environment.
NOTE to programmers in any part of the world that use number decimal separator other than period. The text string input representing a path must be constructed exactly as define in Path Markup Syntax. Decimal numbers with fraction must use period regardless of world region. If you use optional separator between x and y values, it must be a comma. If the string is generated by another application make sure that the PathGeometry.ToString
is called with: PathGeometry.ToString(System.Globalization.CultureInfo.InvariantCulture)
. In other words, The Microsoft's PathGeometry.Parse
method will fail to read a text string produce in Italy, for example, by PathGeometry.ToString
method without IFormatProvider set to InvariantCulture.
There are three methods to define a brush for WPF applications. All of these methods will set the brush opacity at the same time.
-
System.Windows.Media.SolidColorBrush
see SolidColorBrush Class -
System.Windows.Media.LinearGradientBrush
see LinearGradientBrush Class -
System.Windows.Media.RadialGradientBrush
see RadialGradientBrush Class
There are five methods to define a brush for all applications. All of these methods will set the brush opacity at the same time.
- Set brush to
System.Drawing.Color
- Set brush to
PdfAxialShading
- Set brush to
PdfRadialShading
- Set brush to
PdfTilingPattern
- Set brush to
UseCurrentBrush
If you want the DrawWPFPath
class to set the brush to the currently selected brush, call UseCurrentBrush
method.
There is one method to define a pen for WPF applications. Call SetPen
method with System.Windows.Media.Pen
class (See Pen Class). Note, the Pen.Brush
property must be SolidColorBrush
. The pen class contains all required information to draw a line, such as color and width.
There is one method to define a pen for all applications. Call SetPen
with System.Drawing.Color
argument. The color argument defines alpha, red, green and blue components. To set other pen characteristics set any or all of the following properties and methods:
SetPenWidth
DashArray
DashPhase
LineCap
LineJoin
MiterLimit
If you want the DrawWPFPath
class to set the pen to the currently selected pen call UseCurrentPen
method.
Once DrawWPFPath
class is set with all information needed to draw the path, call PdfContents.DrawWPFPath
method. The DrawWPFPath class calculates the transformation matrix required to convert input path to drawing rectangle based on the path bounding box and the drawing rectangle and alignment. Alignment of zero (the default) will stretch the path to fit the drawing area. All other alignment values position the path within the drawing area according to the argument enumeration value.
public void DrawWPFPath(
DrawWPFPath Path, // path to be drawn
double OriginX, // Drawing rectangle in user units left side
double OriginY, // Drawing rectangle in user units bottom side
double Width, // Drawing rectangle in user units width
double Height, // Drawing rectangle in user units height
// path alignment within drawing rectangle
// Alignment=0 means the path will be stretched
// in either horizontal or vertical direction
// to fit the drawing rectangle
ContentAlignment Alignment = 0)
This page is a copy from https://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library by Uzi Granot. The article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL). All rights to the texts and source code remain with Uzi Granot.