C# 将Windows墨水另存为透明PNG图像-缺少高亮笔笔划
我正在尝试在UWP应用程序中加入Windows墨水,并通过调整将绘制的笔划保存为PNG图像(而不是GIF/ISF)开始 因此,XAML视图包括一个C# 将Windows墨水另存为透明PNG图像-缺少高亮笔笔划,c#,xaml,uwp,inkcanvas,win2d,C#,Xaml,Uwp,Inkcanvas,Win2d,我正在尝试在UWP应用程序中加入Windows墨水,并通过调整将绘制的笔划保存为PNG图像(而不是GIF/ISF)开始 因此,XAML视图包括一个Windows.UI.XAML.Controls.InkToolbar和一个Windows.UI.XAML.Controls.InkCanvas,我能够在画布上绘制笔划,并通过以下代码将其保存为图像文件: IReadOnlyList<InkStroke> currentStrokes = inkCanvas.InkPresenter.Str
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);
}
}
即使在这种情况下,文件也会保存,背景是透明的,圆珠笔笔划和铅笔笔划都会正确渲染,但图像结果不包括使用荧光笔工具绘制的任何笔划
有人能解释为什么在这种情况下省略这些笔划吗?是否可以在透明背景上渲染高亮笔划?问题在于高亮笔划是透明的。清除透明的
颜色时。突出显示笔划将不容易检测。
根据您的需要,您可以为InkPresenter
设置新的属性,而不使用属性。DrawAsHighlighter
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());
}
}
}
问题在于高亮显示笔划是透明的。清除透明的颜色时。突出显示笔划将不容易检测。
根据您的需要,您可以为InkPresenter
设置新的属性,而不使用属性。DrawAsHighlighter
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());
}
}
}