Created
May 5, 2015 04:50
-
-
Save davidhagg/4c847213e7600f9bd524 to your computer and use it in GitHub Desktop.
WPF Create Visual with border that appears to be only inside of drawn geometry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static DrawingVisual CreateVisual() | |
{ | |
var noBorderPen = new Pen(new SolidColorBrush(Colors.Transparent), 0); | |
var selectionPen = new Pen(new SolidColorBrush(Colors.Yellow), 20 * 2); | |
var drawingVisual = new DrawingVisual(); | |
using (var dc = drawingVisual.RenderOpen()) | |
{ | |
StreamGeometry geometry = new StreamGeometry(); | |
using (var ctx = geometry.Open()) | |
{ | |
ctx.BeginFigure(new Point(100, 100), true, true); | |
ctx.LineTo(new Point(300, 100), true, false); | |
ctx.LineTo(new Point(300, 200), true, false); | |
} | |
// Draw the geometry | |
dc.DrawGeometry(new SolidColorBrush(Colors.Fuchsia), noBorderPen, geometry); | |
// Apply a clip path to only draw the geometry and hide everything outside | |
dc.PushClip(geometry); | |
// Draw the geometry on top with the selection border | |
dc.DrawGeometry(Brushes.Transparent, selectionPen, geometry); | |
// Remove the clip path | |
dc.Pop(); | |
geometry.Freeze(); | |
} | |
return drawingVisual; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment