簡體   English   中英

將Windows墨水另存為透明的PNG圖像-缺少熒光筆筆觸

[英]Save Windows Ink as transparent PNG image - missing highlighter strokes

我試圖將Windows Ink包含在UWP應用程序中,然后首先通過改編Windows Ink教程應用程序以將繪制的筆划另存為PNG圖像(而不是GIF / ISF)。

因此,XAML視圖包括Windows.UI.Xaml.Controls.InkToolbarWindows.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 ,而這些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());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM