2017-09-14 101 views
0

我試圖在UWP應用程序中包含Windows Ink,並通過調整Windows Ink tutorial app將繪製的筆畫保存爲PNG圖像(而不是GIF/ISF)來啓動。將Windows Ink保存爲透明PNG圖像 - 缺失熒光筆筆畫

因此,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); 

即使在這種情況下,該文件被保存,背景是透明的,圓珠筆筆畫以及鉛筆筆觸被正確呈現 - 但圖像結果不包括使用熒光筆工具繪製的任何筆畫。

有人可以解釋爲什麼這些筆畫在這種情況下被省略?是否有可能通過透明背景呈現熒光筆筆畫?

回答

2

問題是高光筆畫是透明的。當你清除顏色Transparent。高光筆畫將很難被檢測到。 對於您的要求,您可以設置新的attributes,其中爲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()); 
     } 
    } 
} 
+0

謝謝,很好的解決方法! – andreask