DrawTool paint texture pen and drawtool paint texture
First:
Public class TextureStroke: Stroke {private System. windows. media. imageSource imageSource _; // texture image private string textureFile _; public TextureStroke (StylusPointCollection stylusPoints, DrawingAttributes da, string file): base (stylusPoints, da) {this. textureFile _ = file; this. imageSource _ = new System. windows. media. imaging. bitmapImage (new Uri (this. textureFile _));} /// <summary> /// implement plotting /// </summary> /// <param name = "drawingContext"> </param> /// <param name = "drawingAttributes"> </param> protected override void DrawCore (DrawingContext drawingContext, drawingAttributes drawingAttributes) {double width = drawingAttributes. width * 2.0; System. windows. media. streamGeometry streamGeometry = new System. windows. media. streamGeometry (); using (System. windows. media. streamGeometryContext streamGeometryContext = streamGeometry. open () {streamGeometryContext. beginFigure (Point) base. stylusPoints [0], false, false); foreach (System. windows. input. stylusPoint current in base. stylusPoints) {streamGeometryContext. lineTo (Point) current, true, true);} streamGeometryContext. close ();} DrawTexture (streamGeometry, this. imageSource _, drawingContext, width, this. textureFile _);} public static void DrawTexture (Geometry geometry, ImageSource imageSource, DrawingContext drawingContext, double width, string imagePath) {Rect rc = geometry. bounds; DrawingBrush drawingBrush = new DrawingBrush (new ImageDrawing {Rect = rc, ImageSource = imageSource}); drawingBrush. tileMode = TileMode. tile; Uri uriSource = new Uri (imagePath); BitmapImage bitmapImage = new BitmapImage (uriSource); drawingBrush. viewport = new Rect (0.0, 0.0, (double) bitmapImage. pixelWidth, (double) bitmapImage. pixelHeight); drawingBrush. viewportUnits = BrushMappingMode. absolute; PathGeometry widenedPathGeometry = geometry. getWidenedPathGeometry (new Pen (null, width) {StartLineCap = PenLineCap. round, EndLineCap = PenLineCap. round}); Pen pen = new Pen (drawingBrush, 80.0); drawingContext. pushClip (widenedPathGeometry); drawingContext. drawRectangle (drawingBrush, pen, rc); drawingContext. pop ();}}
2. instance applications
Here we will use the multi-touch canvas introduced in the previous article to modify the generation of the stroke object in the _ startStroke method. The corresponding code is as follows:
Bool isTexturePen = true; // The private variable private void _ startStroke (object device, Point inputPosition) defined externally ){//... {some code} // = is the default stroke or custom stroke Stroke stroke = isTexturePen determined based on the variable isTexturePen? New TextureStroke (stylusPointCollection, this. _ inkCanvas. defadradrawingattributes, "c: \ 1.png"): new Stroke (stylusPointCollection); // end //... {some code }}
3. End
The Code involved in the DrawTool series is modified and only applicable to the demo scenario. You can modify the code yourself if you need it, including performance adjustments.