DrawTool paint texture pen and drawtool paint texture

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.