将Windows Ink保存为透明PNG图像 - 缺失荧光笔笔画
问题描述:
我试图在UWP应用程序中包含Windows Ink,并通过调整Windows Ink tutorial app将绘制的笔画保存为PNG图像(而不是GIF/ISF)来启动。将Windows Ink保存为透明PNG图像 - 缺失荧光笔笔画
因此,XAML视图包括Windows.UI.Xaml.Controls.InkToolbar
和Windows.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);
即使在这种情况下,该文件被保存,背景是透明的,圆珠笔笔画以及铅笔笔触被正确呈现 - 但图像结果不包括使用荧光笔工具绘制的任何笔画。
有人可以解释为什么这些笔画在这种情况下被省略?是否有可能通过透明背景呈现荧光笔笔画?
答
问题是高光笔画是透明的。当你清除颜色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());
}
}
}
谢谢,很好的解决方法! – andreask