none
WPF如何实现软笔效果? RRS feed

全部回复

  • 你好,

    我们可以使用InkCanvas 来实现类似的软笔效果, 下面有个简单的Demo,你可以参考一下

    #Xaml

    <Window x:Class="WpfApp1.InkCanvasDemo"
    
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    
            xmlns:local="clr-namespace:WpfApp1"
    
            mc:Ignorable="d"
    
            Title="InkCanvasDemo" Height="300" Width="300">
    
        <Grid>
    
            <InkCanvas x:Name="inkCanvas"></InkCanvas>
    
        </Grid>
    
    </Window>
    

    #Code Behind

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Windows;
    
    using System.Windows.Controls;
    
    using System.Windows.Data;
    
    using System.Windows.Documents;
    
    using System.Windows.Ink;
    
    using System.Windows.Input;
    
    using System.Windows.Media;
    
    using System.Windows.Media.Imaging;
    
    using System.Windows.Shapes;
    
    
    namespace WpfApp1
    
    {
    
        /// <summary>
    
        /// Interaction logic for InkCanvasDemo.xaml
    
        /// </summary>
    
        public partial class InkCanvasDemo : Window
    
        {
    
            DrawingAttributes drawingAttributes;
    
            public InkCanvasDemo()
    
            {
    
                InitializeComponent();
    
    
                //创建 DrawingAttributes 类的一个实例  
    
                drawingAttributes = new DrawingAttributes();
    
                //将 InkCanvas 的 DefaultDrawingAttributes 属性的值赋成创建的 DrawingAttributes 类的对象的引用  
    
                //InkCanvas 通过 DefaultDrawingAttributes 属性来获取墨迹的各种设置,该属性的类型为 DrawingAttributes 型  
    
                inkCanvas.DefaultDrawingAttributes = drawingAttributes;
    
    
                //设置 DrawingAttributes 的 Color 属性设置颜色  
    
                drawingAttributes.Color = Colors.Red;
    
                //利用 DrawingAttributes 的 Width 和 Height 属性来设置墨迹的宽度和高度  
           
                drawingAttributes.Width = 20;
    
                drawingAttributes.Height = 10;
    
                //利用 DrawingAttributes 的StylusTip 属性可以设置墨迹触笔的形状,默认值是 StylusTip.Ellipse  
    
                //将墨迹的宽度和高度都设置为稍大一些可以清楚的看到差别,如果较小则不太容易看出差别  
    
                drawingAttributes.StylusTip = StylusTip.Rectangle;
    
    
                //将 FitToCurve 属性设置为 true 会在你绘制完一次墨迹后自动利用贝塞尔曲线来对你的墨迹进行平滑处理  
    
                 drawingAttributes.FitToCurve = true;
    
    
                //设置 IsHighlighter 属性为 true ,则墨迹显示的时候看上去像是荧光笔  
    
                //使 Stroke 变的透明一些
                drawingAttributes.IsHighlighter = false;
    
                //设置 IgnorePressure 属性为 true墨迹粗细会随压力的增大而增大  
    
               drawingAttributes.IgnorePressure = true;
    
    
            }
    
        }
    
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月23日 5:05
  • 非常感谢您的回复,是我想要的答案,就是画完以后会抖(闪)一下,不知道为什么?
    2018年1月23日 6:42
  • 你好,

    我估计应该是反锯齿的原因导致的,你可以把代码改成下面这样试试。

    #Xaml

    <InkCanvas x:Name="inkCanvas" SnapsToDevicePixels="False" RenderOptions.EdgeMode="Aliased"></InkCanvas>

    #Code Behind 改这句

    drawingAttributes.StylusTip = StylusTip.Ellipse;

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月23日 8:11
  • 您好,根据你说的,我试过了,效果还没有之前的好,我试着把FitToCurve去掉后,就没有画完闪的效果,但是去掉画出来的线就不平滑了,有没有更好的办法达到效果呢?
    2018年1月24日 1:42
  • 你好,

    >>您好,根据你说的,我试过了,效果还没有之前的好,我试着把FitToCurve去掉后,就没有画完闪的效果,但是去掉画出来的线就不平滑了,有没有更好的办法达到效果呢?

    据我所知, 平滑的效果只有利用贝塞尔曲线来对你的墨迹进行处理.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月24日 1:47
  • 我看别人做这个软笔的时候不会抖动,不知道是如何实现的?
    2018年1月28日 3:17
  • 你好,

    你说的抖动是像什么样的抖动? 可以做一个类似gif 动画吗? 我本机测试不是很明显。

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月29日 9:51