1. inkcanvas class.
Inkcanvas is required to implement the canvas. Generally, noCodeYou can draw a line on it.
If you need to set the paint brush color and pen tip size, you need to set defadradrawingattributes, for example:
Drawingattributes attributes = new drawingattributes (); Attributes. Color = colors. Black; Attributes. Height = 50; Attributes. width = 50; Attributes. fittocurve = true;
2. draw a straight line on inkcanvas
Method: Modify the settings in the strokecollected event. The strokecollected event is triggered after a single stroke ends. You can extract the starting point and endpoint of a stroke, and then use these two points to create a new stroke.
The Code is as follows:
private void inkcanvas_strokecollected (Object sender, inkcanvasstrokecollectedeventargs e) {If (E. stroke. styluspoints. count> 1) {updateline (E. stroke) ;}} private void updateline (stroke currentstroke) {styluspoint beginpoint = currentstroke. styluspoints [0]; // start point styluspoint endpoint = currentstroke. styluspoints. last (); // end point packagecanvas. strokes. remove (currentstroke); // remove the original stroke list
pointlist = new list
(); pointlist. add (new point (beginpoint. x, beginpoint. y); pointlist. add (new point (endpoint. x, endpoint. y); styluspointcollection point = new styluspointcollection (pointlist); stroke = new stroke (point); // use two points to implement the stroke. drawingattributes = packagecanvas. defaultdrawingattributes. clone (); inkcanvas. strokes. add (stroke) ;}
3. Draw a straight dotted line on inkcanvas
Method: Take the start point and end point as above. The difference is: draw many points between the two points, and then connect the two adjacent points into a stroke. This straight dotted line is changed.
The Code is as follows:
Private void updateline (stroke currentstroke) {inkcanvas. Strokes. Remove (currentstroke); // remove the original stroke int dottime = 0;
Int intervallen = 6; // Step Double linelen = math. SQRT (math. pow (beginpoint. x-endpoint. x, 2) + math. pow (beginpoint. y-endpoint. y, 2); // line length point currentpoint = new point (beginpoint. x, beginpoint. y); double relativarate = math. ABS (endpoint. y-beginpoint. y) * 1.0/math. ABS (endpoint. x-beginpoint. x); double angle = math. atan (relativarate) * 180/math. pi; // The angle of the straight line. You do not need to consider plus or minus int xorientation = endpoint. X> beginpoint. X? 1:-1; // judge the X axis direction of the new vertex int yorientation = endpoint. Y> beginpoint. Y? 1:-1; if (linelen <intervallen) {return;} while (dottime * intervallen <linelen) {Double X = currentpoint. X + dottime * intervallen * Math. cos (angle * Math. PI/180) * xorientation; Double Y = currentpoint. Y + dottime * intervallen * Math. sin (angle * Math. PI/180) * yorientation; List <point> PL = new list <point> (); PL. add (new point (x, y); x + = intervallen * Math. cos (angle * Math. PI/180) * xorientation; y + = intervallen * Math. sin (angle * Math. PI/180) * yorientation; PL. add (new point (x, y); styluspointcollection SPC = new styluspointcollection (PL); // The adjacent two points are used as a stroke = new stroke (SPC); stroke. drawingattributes = packagecanvas. defaultdrawingattributes. clone (); inkcanvas. strokes. add (stroke); dottime + = 2 ;}}
4. Draw a dotted line on the inkcanvas
Method: Obtain the coordinates of online points in the parameter attribute of the inkcanvas_strokecollected method.
2) generate styluspointcollection collection = currentstroke. styluspoints to store all points of the current stroke.
2) generate a list <point> allpointlist to store the final vertex.
2) set the starting point to "allpointlist", the starting point to "currentpoint ".
3) traverse the collection, if currentpoint and find the point (item) distance = step then item => allpointlist; item => currentpoint, take the next point
If currentpoint and locate point (item)> step then finds a point on the currentpoint and item line, so that the distance from currentpoint = step, item => allpointlist; item => currentpoint, continue current point
If currentpoint and the distance from the found point (item) <step size takes the next point
The Code is as follows:
Private void updateline (stroke currentstroke) {inkcanvas. strokes. remove (currentstroke); styluspointcollection collection = currentstroke. styluspoints; List <point> allselectedpoint = new list <point> (); point currentpoint = new point (Collection [0]. x, Collection [0]. y); allselectedpoint. add (currentpoint); For (INT I = 0; I <collection. count; I ++) {var item = Collection [I]; double length = math. SQRT (MA Th. pow (item. x-currentpoint. x, 2) + math. pow (item. y-currentpoint. y, 2); If (INT) (Length + 0.5) = (INT) intervallen | length = intervallen) {currentpoint = new point (item. x, item. y); allselectedpoint. add (currentpoint);} else if (length> intervallen) {double relativarate = math. ABS (item. y-currentpoint. y) * 1.0/math. ABS (item. x-currentpoint. x); double angle = math. atan (relativarate )* 180/Math. Pi; int xorientation = item. x> currentpoint. X? 1:-1; int yorientation = item. Y> currentpoint. Y? 1:-1; Double X = currentpoint. X + intervallen * Math. cos (angle * Math. PI/180) * xorientation; Double Y = currentpoint. Y + intervallen * Math. sin (angle * Math. PI/180) * yorientation; currentpoint = new point (x, y); allselectedpoint. add (currentpoint); I --; // very important. Continue to the current vertex }}for (Int J = 0; j <allselectedpoint. count; j ++) {list <point> P = new list <point> (); p. add (allselectedpoint [J]); If (j <allselectedpoint. count-1) {J ++;} p. add (allselectedpoint [J]); styluspointcollection SPC = new styluspointcollection (p); stroke = new stroke (SPC); inkcanvas. strokes. add (stroke );}}
the above is the tested code. The disadvantage is that the Code can only be corrected after the current stroke is completed because it is written in inkcanvas_strokecollected.