Skip to content

2.20 Windows Presentation Foundation WPF

OgreTransporter edited this page Mar 2, 2020 · 2 revisions

Drawing WPF graphics is a four steps process.

  1. Create DrawWPFPath object. Input arguments are path and Y axis direction.
  2. Optionally add a brush by calling one of seven SetBrush methods or call UseCurrentBrush method.
  3. Optionally add a pen by calling one of two SetPen methods or call UseCurrentPen method.
  4. 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.

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)
Clone this wiki locally