Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用inkPresenter以不透明度绘制彼此的笔划?_C#_.net_Wpf - Fatal编程技术网

C# 如何使用inkPresenter以不透明度绘制彼此的笔划?

C# 如何使用inkPresenter以不透明度绘制彼此的笔划?,c#,.net,wpf,C#,.net,Wpf,我想使用inkpresenter,这样每一个新笔划都会放置在前一个笔划之上,从而产生一种变暗的效果 我每次都尝试创建一个新的inkpresenter并将其添加到画布中,但不透明效果只有在释放鼠标后才会出现。我希望它能在你画的时候立即显示出来。如果我直接将inkpresenter设置为目标元素的内容(无混合),则笔划在绘制时具有不透明度。您尚未发布任何代码来显示当前在应用程序中如何处理inkpresenter。我已经在WPf中做了一个快速测试程序,它似乎可以正确地处理不透明度 我将InkPrese

我想使用inkpresenter,这样每一个新笔划都会放置在前一个笔划之上,从而产生一种变暗的效果


我每次都尝试创建一个新的inkpresenter并将其添加到画布中,但不透明效果只有在释放鼠标后才会出现。我希望它能在你画的时候立即显示出来。如果我直接将inkpresenter设置为目标元素的内容(无混合),则笔划在绘制时具有不透明度。

您尚未发布任何代码来显示当前在应用程序中如何处理inkpresenter。我已经在WPf中做了一个快速测试程序,它似乎可以正确地处理不透明度

我将
InkPresenter
控件放置在xaml中,并将
PreviewMouseDown
PreviewMouseMove
PreviewMouseUp
方法”处理程序添加到后面的代码中。我用来处理这些事件的代码如下所示:

System.Windows.Ink.Stroke newStroke = null;

    private void inkPresenterSample_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        inkPresenterSample.CaptureMouse();

        //get mouse position and add first point to a new line to be drawn
        var mousePosition = e.GetPosition(inkPresenterSample);
        var stylusStartPoint = new StylusPointCollection();
        stylusStartPoint.Add(new StylusPoint(mousePosition.X, mousePosition.Y));

        //set line's attributes, it real application this should be probably done outside this method
        var drawingAttributes = new System.Windows.Ink.DrawingAttributes();
        //IMPORTANT: semi-transparent color is used, so the opacity effect is visible
        drawingAttributes.Color = System.Windows.Media.Color.FromArgb(110, 0, 0, 0);
        drawingAttributes.Width = 10;

        //create a new stroke to be drawn
        newStroke = new System.Windows.Ink.Stroke(stylusStartPoint, drawingAttributes);
        newStroke.StylusPoints.Add(new StylusPoint(mousePosition.X, mousePosition.Y));

        //add reference to a new stroke to the InkPresenter control
        inkPresenterSample.Strokes.Add(newStroke);
    }

    private void inkPresenterSample_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        inkPresenterSample.ReleaseMouseCapture();

        if (newStroke != null)
        {
            newStroke = null;
        }
    }

    private void inkPresenterSample_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        //if a stroke is currently drawn in the InkPresenter
        if (newStroke != null)
        {
            //add a new point to the stroke
            var mousePosition = e.GetPosition(inkPresenterSample);
            newStroke.StylusPoints.Add(new StylusPoint(mousePosition.X, mousePosition.Y));
        }
    }
它似乎像你描述的那样工作,我可以看到线条的较暗部分,其中很少有重叠

更新

建议的解决方案中的重叠效果适用于多条重叠线,但如果一条线本身重叠,则不起作用。如果您希望在这种情况下也能正常工作,您可以尝试:

  • 使用
    画布
    并在其上添加
    多段线
    元素(更新:这似乎与建议的第一个解决方案类似,因此一行重叠不会产生不透明效果)
  • 使用此处描述的
    DrawingContext
    查看在
    Image
    元素上绘制,它可能会有帮助:

请添加一些代码,说明您如何在代码中使用InkPresenter。我写了一个简单的测试应用程序,它似乎工作正常。非常感谢!你的代码真的很有效。我认为主要的区别在于我在mouseup上添加了笔划而不是mousedown,并且我在inkpresenter上设置了DrawingAttribute而不是笔划。