将 Windows 墨水另存为透明 PNG 图像 - 缺少荧光笔笔触

Save Windows Ink as transparent PNG image - missing highlighter strokes

我正在尝试在 UWP 应用程序中包含 Windows Ink,并开始调整 Windows Ink tutorial app 以将绘制的笔画保存为 PNG 图像(而不是 GIF / ISF)。

因此,XAML 视图包含一个 Windows.UI.Xaml.Controls.InkToolbar 和一个 Windows.UI.Xaml.Controls.InkCanvas,我可以在 Canvas 上画笔画并将其保存为图像通过以下代码文件:

IReadOnlyList<InkStroke> currentStrokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
if (currentStrokes.Count > 0)
{
    StorageFile file;
    // Using a file picker to identify the target file -> omitted this part
    if (file != null)
    {
        CanvasDevice device = CanvasDevice.GetSharedDevice();
        CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96);

        using (var ds = renderTarget.CreateDrawingSession())
        {
            ds.Clear(Colors.White);
            ds.DrawInk(currentStrokes);
        }
        using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Png, 1f);
    }
}

到目前为止一切正常。现在,我想保存透明背景的图像,并更改了以下行:

ds.Clear(Colors.Transparent);

即使在这种情况下,文件也会被保存,背景是透明的,圆珠笔和铅笔笔画都会被正确渲染 - 但图像结果不包括使用 荧光笔绘制的任何笔画 工具。

谁能解释一下为什么在这种情况下省略了这些笔画?是否有可能以某种方式在透明背景上渲染荧光笔笔触?

问题是突出显示笔划是透明的。当您清除 Transparent 颜色时。不容易检测到高亮笔划。 根据您的要求,您可以设置新的 attributes,而 InkPresenter 没有 attributes.DrawAsHighlighter

private void SetHighLight()
{
  InkDrawingAttributes drawingAttributes = 
inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
  InkDrawingAttributes attributes = new InkDrawingAttributes();
  attributes.PenTip = PenTipShape.Rectangle;
  attributes.Size = new Size(4, 10);
  attributes.Color = drawingAttributes.Color;
  inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(attributes);
}

在调用 DrawInk 之前添加一个新图层并赋予它不透明度。并专门为荧光笔制作了 0.5 不透明度的 inkCanvas,看起来就像你在使用荧光笔。

private void GetCanvasRender(out CanvasRenderTarget renderTarget, float opacity)
{
    CanvasDevice device = CanvasDevice.GetSharedDevice();
    renderTarget = new CanvasRenderTarget(device, (int)ink.ActualWidth, (int)ink.ActualHeight, 96);
    using (var ds = renderTarget.CreateDrawingSession())
    {
        ds.Clear(Colors.Transparent);
        using (ds.CreateLayer(opacity))
        {
            ds.DrawInk(ink.InkPresenter.StrokeContainer.GetStrokes());
        }
    }
}