将Windows Ink保存为透明PNG图像 - 缺少突出显示的笔触

时间:2017-09-14 17:18:53

标签: c# xaml uwp inkcanvas win2d

我正在尝试将Windows Ink包含在UWP应用中,并通过调整Windows Ink tutorial app将绘制的笔划保存为PNG图像(而不是GIF / ISF)来启动。

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

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

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

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

1 个答案:

答案 0 :(得分:3)

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

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