I want to create wavy underlines using TextDecoration (in a control similar to RichTextBox).
I did the following thing:
private static Pen CreateErrorPen() {
var geometry = new StreamGeometry();
using (var context = geometry.Open()) {
context.BeginFigure(new Point(0.0, 0.0), false, false);
context.PolyLineTo(new[] {
new Point(0.75, 0.75),
new Point(1.5, 0.0),
new Point(2.25, 0.75),
new Point(3.0, 0.0)
}, true, true);
}
var brushPattern = new GeometryDrawing {
Pen = new Pen(Brushes.Red, 0.2),
Geometry = geometry
};
var brush = new DrawingBrush(brushPattern) {
TileMode = TileMode.Tile,
Viewport = new Rect(0.0, 1.5, 9.0, 3.0),
ViewportUnits = BrushMappingMode.Absolute
};
var pen = new Pen(brush, 3.0);
pen.Freeze();
return pen;
}
This almost works, but depending on the underlined word position in text, underlines often show up as a pattern of several superimposed waves. Also the underlines are a bit blurry even when they are correct (wpf problem with drawing between pixels, I suppose).
My solution was kind of a trial-and-error, so I might have gone a wrong way, especially with Viewport/ViewportUnits.
What am I doing wrong and is there a way to get crisp underlines?
Thanks in advance.