如何使用inkPresenter以不透明度相互绘制笔划?

时间:2012-05-30 18:27:11

标签: c# .net wpf

我想使用一个墨水预告器,以便每个新笔划都放在上面的笔划之上,从而产生一个变暗的效果。

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

1 个答案:

答案 0 :(得分:0)

您尚未发布任何代码,显示您当前在应用程序中处理InkPresenter的方式。我在WPf中制作了一个快速测试程序,它似乎与不透明度一起正常工作。

我将InkPresenter控件放在xaml中,并将PreviewMouseDownPreviewMouseMovePreviewMouseUp方法'处理程序添加到后面的代码中。我用来处理这些事件的代码如下:

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));
        }
    }

它似乎按照你的描述工作,我可以看到线条的较暗部分,其中几乎没有重叠。

<强>更新

建议解决方案中的重叠效果适用于多个重叠线,但如果单个线重叠自身则不起作用。如果你想在这种情况下使它工作,你可以尝试:

  • 使用Canvas并在其上添加Polyline元素(更新:这似乎与建议的第一个解决方案一样,因此一行重叠不会产生不透明效果)
  • 使用此处所述的Image查看DrawingContext元素上的绘图,它可能有所帮助:Drawing with mouse causes gaps between pixels