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