PETZOLD BOOK BLOG

Charles Petzold on writing books, reading books, and exercising the internal UTM


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

Variable-Width Lines in WPF

December 30, 2007
Roscoe, N.Y.

Occasionally a question comes up in the MSDN Forum for WPF programming about rendering variable-width lines.

Variable-width lines are not really lines, but actually paths that describe the contour of the line. The frame around this portrait was designed using this technique:

The source code is here to run or download:

VariableWidthLines.xaml

If you're interested in rendering interesting variations of lines, be on the lookout for my WPF column in the April 2008 issue of MSDN Magazine.

Another technique (particularly suited for generating variable-width lines in code and when the variation is more subtle) makes use of the stylus and ink API in the System.Windows.Ink and System.Windows.Input namespaces. You create a Stroke object with the StylusPoints property set to a StylusPointCollection object containing a collection of StylusPoint objects. You're basically defining the line as a polyline but each StylusPoint object has a PressureFactor property as well as X and Y properties. Set PressureFactor to a value between 0 and 1 to set the stroke width at that point.

Call the GetGeometry method of Stroke to convert the stroke into a Geometry object, which you can then set to the Data property of a Path object.


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold
www.charlespetzold.com