小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

wpf inkcanvas customink 毛筆效果

 鴻蛟家平 2017-08-11
 聯(lián)系QQ:715705486 18662431387
用wpf來做毛筆書寫效果,需要自定義inkcanvas,基本效果可以,就是連續(xù)畫很長的筆觸時(shí)會(huì)有卡頓現(xiàn)象。解決方法,做一個(gè)判斷,當(dāng)筆觸超過一定長度則停止。
核心代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;
using System.Windows.Ink;
using System.Windows.Media.Imaging;

namespace WpfApplication1
{
    class CustomRenderingInkCanvas:InkCanvas
    {
        CustomDynamicRenderer customRenderer = new CustomDynamicRenderer();

        public CustomRenderingInkCanvas()
            : base()
        {
            // Use the custom dynamic renderer on the
            // custom InkCanvas.
            this.DynamicRenderer = customRenderer;
        }

        protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
        {
            // Remove the original stroke and add a custom stroke.
            this.Strokes.Remove(e.Stroke);
            CustomStroke customStroke = new CustomStroke(e.Stroke.StylusPoints);
            this.Strokes.Add(customStroke);

            // Pass the custom stroke to base class' OnStrokeCollected method.
            InkCanvasStrokeCollectedEventArgs args =
                new InkCanvasStrokeCollectedEventArgs(customStroke);
            base.OnStrokeCollected(args);

        }
    }
    // A class for rendering custom strokes
    class CustomStroke : Stroke
    {
        Brush brush;
        Pen pen;

        public CustomStroke(StylusPointCollection stylusPoints)
            : base(stylusPoints)
        {
            // Create the Brush and Pen used for drawing.
            brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 10d);
            pen = new Pen(brush, 2d);
        }

        protected override void DrawCore(DrawingContext drawingContext,
                                         DrawingAttributes drawingAttributes)
        {
            ImageSource img = new BitmapImage(new Uri(@"1.png", UriKind.RelativeOrAbsolute));

            //前一個(gè)點(diǎn)的繪制。
            Point prevPoint = new Point(double.NegativeInfinity,
                                        double.NegativeInfinity);


            int w = 10 + 15;    //輸出時(shí)筆刷的實(shí)際大小


            Point pt = new Point(0, 0);
            Vector v = new Vector();            //前一個(gè)點(diǎn)與當(dāng)前點(diǎn)的距離
            var subtractY = 0d;                 //當(dāng)前點(diǎn)處前一點(diǎn)的Y偏移
            var subtractX = 0d;                 //當(dāng)前點(diǎn)處前一點(diǎn)的X偏移
            var pointWidth = 10;
            double x = 0, y = 0;
            for (int i = 0; i < this.StylusPoints.Count; i++)
            {
                pt = (Point)this.StylusPoints[i];
                v = Point.Subtract(prevPoint, pt);

                // Debug.WriteLine("X " + pt.X + "\t" + pt.Y);

                subtractY = (pt.Y - prevPoint.Y) / v.Length;    //設(shè)置stylusPoints兩個(gè)點(diǎn)之間需要填充的XY偏移
                subtractX = (pt.X - prevPoint.X) / v.Length;

                if (w - v.Length < 10)          //控制筆刷大小
                {
                    pointWidth = 10;
                }
                else
                {
                    pointWidth = w - Convert.ToInt32(v.Length);                  //在兩個(gè)點(diǎn)距離越大的時(shí)候,筆刷所展示的大小越小
                }


                for (double j = 0; j < v.Length; j = j + 1d)    //填充stylusPoints兩個(gè)點(diǎn)之間
                {
                    x = 0; y = 0;

                    if (prevPoint.X == double.NegativeInfinity || prevPoint.Y == double.NegativeInfinity || double.PositiveInfinity == prevPoint.X || double.PositiveInfinity == prevPoint.Y)
                    {
                        y = pt.Y;
                        x = pt.X;
                    }
                    else
                    {
                        y = prevPoint.Y + subtractY;
                        x = prevPoint.X + subtractX;
                    }

                    drawingContext.DrawImage(img, new Rect(x - pointWidth / 2, y - pointWidth / 2, pointWidth, pointWidth));    //在當(dāng)前點(diǎn)畫筆刷圖片
                    prevPoint = new Point(x, y);


                    if (double.IsNegativeInfinity(v.Length) || double.IsPositiveInfinity(v.Length))
                    { break; }
                }
            }
          //  this.StylusPoints = null;
            //// Allocate memory to store the previous point to draw from.
            //Point prevPoint = new Point(double.NegativeInfinity,
            //                            double.NegativeInfinity);

            //// Draw linear gradient ellipses between 
            //// all the StylusPoints in the Stroke.
            //for (int i = 0; i < this.StylusPoints.Count; i++)
            //{
            //    Point pt = (Point)this.StylusPoints[i];
            //    Vector v = Point.Subtract(prevPoint, pt);

            //    // Only draw if we are at least 4 units away 
            //    // from the end of the last ellipse. Otherwise, 
            //    // we're just redrawing and wasting cycles.
            //    if (v.Length > 4)
            //    {
            //        // Set the thickness of the stroke 
            //        // based on how hard the user pressed.
            //        double radius = this.StylusPoints[i].PressureFactor * 10d;
            //        drawingContext.DrawEllipse(brush, pen, pt, radius, radius);
            //        prevPoint = pt;
            //    }
            //}
        }
    }
    class CustomDynamicRenderer : DynamicRenderer
    {
        [ThreadStatic]
        static private Brush brush = null;

        [ThreadStatic]
        static private Pen pen = null;

        private Point prevPoint;

        protected override void OnStylusDown(RawStylusInput rawStylusInput)
        {
            // Allocate memory to store the previous point to draw from.
            prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
            base.OnStylusDown(rawStylusInput);
        }

        protected override void OnDraw(DrawingContext drawingContext,
                                       StylusPointCollection stylusPoints,
                                       Geometry geometry, Brush fillBrush)
        {
            ImageSource img = new BitmapImage(new Uri(@"1.png",UriKind.RelativeOrAbsolute));

            //前一個(gè)點(diǎn)的繪制。
            Point prevPoint = new Point(double.NegativeInfinity,
                                        double.NegativeInfinity);


            int w = 10 + 15;    //輸出時(shí)筆刷的實(shí)際大小


            Point pt = new Point(0, 0);
            Vector v = new Vector();            //前一個(gè)點(diǎn)與當(dāng)前點(diǎn)的距離
            var subtractY = 0d;                 //當(dāng)前點(diǎn)處前一點(diǎn)的Y偏移
            var subtractX = 0d;                 //當(dāng)前點(diǎn)處前一點(diǎn)的X偏移
            var pointWidth = 10;
            double x = 0, y = 0;
            for (int i = 0; i < stylusPoints.Count; i++)
            {
                pt = (Point)stylusPoints[i];
                v = Point.Subtract(prevPoint, pt);

               // Debug.WriteLine("X " + pt.X + "\t" + pt.Y);

                subtractY = (pt.Y - prevPoint.Y) / v.Length;    //設(shè)置stylusPoints兩個(gè)點(diǎn)之間需要填充的XY偏移
                subtractX = (pt.X - prevPoint.X) / v.Length;

                if (w - v.Length < 10)          //控制筆刷大小
                {
                    pointWidth = 10;
                }
                else
                {
                    pointWidth = w - Convert.ToInt32(v.Length);                  //在兩個(gè)點(diǎn)距離越大的時(shí)候,筆刷所展示的大小越小
                }


                for (double j = 0; j < v.Length; j = j + 1d)    //填充stylusPoints兩個(gè)點(diǎn)之間
                {
                    x = 0; y = 0;

                    if (prevPoint.X == double.NegativeInfinity || prevPoint.Y == double.NegativeInfinity || double.PositiveInfinity == prevPoint.X || double.PositiveInfinity == prevPoint.Y)
                    {
                        y = pt.Y;
                        x = pt.X;
                    }
                    else
                    {
                        y = prevPoint.Y + subtractY;
                        x = prevPoint.X + subtractX;
                    }

                    drawingContext.DrawImage(img, new Rect(x - pointWidth / 2, y - pointWidth / 2, pointWidth, pointWidth));    //在當(dāng)前點(diǎn)畫筆刷圖片
                    prevPoint = new Point(x, y);


                    if (double.IsNegativeInfinity(v.Length) || double.IsPositiveInfinity(v.Length))
                    { break; }
                }
            }
            stylusPoints = null;
            //// Create a new Brush, if necessary.
            //if (brush == null)
            //{
            //    brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20d);
            //}

            //// Create a new Pen, if necessary.
            //if (pen == null)
            //{
            //    pen = new Pen(brush, 2d);
            //}

            //// Draw linear gradient ellipses between 
            //// all the StylusPoints that have come in.
            //for (int i = 0; i < stylusPoints.Count; i++)
            //{
            //    Point pt = (Point)stylusPoints[i];
            //    Vector v = Point.Subtract(prevPoint, pt);

            //    // Only draw if we are at least 4 units away 
            //    // from the end of the last ellipse. Otherwise, 
            //    // we're just redrawing and wasting cycles.
            //    if (v.Length > 4)
            //    {
            //        // Set the thickness of the stroke based 
            //        // on how hard the user pressed.
            //        double radius = stylusPoints[i].PressureFactor * 10d;
            //        drawingContext.DrawEllipse(brush, pen, pt, radius, radius);
            //        prevPoint = pt;
            //    }
            //}
        }
    }
}
作者:yz qq:715705486
效果圖如下:

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多