将 Windows Ink 保存为透明的 PNG 图像 - 缺少荧光笔描边 [英] Save Windows Ink as transparent PNG image - missing highlighter strokes

查看:23
本文介绍了将 Windows Ink 保存为透明的 PNG 图像 - 缺少荧光笔描边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Windows Ink 包含在 UWP 应用程序中,并通过调整 Windows Ink 教程应用 将绘制的笔画保存为 PNG 图像(而不是 GIF/ISF).

I'm trying to include Windows Ink in a UWP app, and started by adapting the Windows Ink tutorial app to save the drawn strokes as PNG image (instead of GIF / ISF).

因此,XAML 视图包括一个 Windows.UI.Xaml.Controls.InkToolbar 和一个 Windows.UI.Xaml.Controls.InkCanvas,我可以通过如下代码在Canvas上绘制笔触并保存为图片文件:

So, the XAML view includes a Windows.UI.Xaml.Controls.InkToolbar and a Windows.UI.Xaml.Controls.InkCanvas, and I'm able to draw strokes on the Canvas and save it as an image file through the following code:

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

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

Everything works fine so far. Now, I'd like to save the image with transparent background, and changed the following line:

ds.Clear(Colors.Transparent);

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

Even in this case, the file is saved, the background is transparent, and ballpoint strokes as well as pencil strokes are correctly rendered - but the image result does not include any strokes drawn with the Highlighter tool.

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

Can somebody explain why these strokes are omitted in this case? Is it possible somehow to render Highlighter strokes over a transparent background?

推荐答案

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

The Problem is the highlight strokes are transparent. When you clear the Transparent color. The highlight strokes will be detected not easily. For you requirement,you can set new attributes that with out the attributes.DrawAsHighlighter for 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,看起来就像您在使用荧光笔一样.

Add a new layer before calling DrawInk and gave it an opacity. And made inkCanvas with 0.5 opacity specially for the highlighter, looking like you're using a highlighter.

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

这篇关于将 Windows Ink 保存为透明的 PNG 图像 - 缺少荧光笔描边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆