我想使用一个 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));
}
}
它似乎像你描述的那样工作,我可以看到线条的较暗部分,其中很少有重叠。
更新
建议解决方案中的重叠效果适用于多条重叠线,但如果单条线自身重叠则不起作用。如果你想让它在这种情况下也能工作,你可以尝试:
Canvas
并在其上添加Polyline
元素(更新:这似乎与建议的第一个解决方案一样工作,因此一条线重叠不会产生不透明效果)Image
element using DrawingContext
described here, it may help: Drawing with mouse causes gaps between pixels