gpt4 book ai didi

xaml - 通用 Windows InkCanvas 笔触在 RenderTargetBitmap.RenderAsync 上消失

转载 作者:行者123 更新时间:2023-12-01 00:56:10 24 4
gpt4 key购买 nike

我尝试在 Windows 10 通用应用程序中将 InkCanvas 的笔画渲染到 RenderTargetBitmap。这是我的 xaml 代码:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="10" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid x:Name="container">
<Rectangle Fill="LightBlue" />
<InkCanvas x:Name="InkCanvas" />
</Grid>

<Image Grid.Row="2" x:Name="TheImage" />

<Button Grid.Row="3" Content="CopyToRendertargt" Click="Button_Click" />

</Grid>

这是我设置 Image.Source 属性的代码:

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
InkCanvas.InkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Mouse;
}


private async void Button_Click(object sender, RoutedEventArgs e)
{
RenderTargetBitmap renderTarget = new RenderTargetBitmap();

await renderTarget.RenderAsync(this.container);

this.TheImage.Source = renderTarget;
}

}

当我单击按钮时,我在 InkCanvas 上所做的笔划消失并且 InkCanvas 被卡住,直到我调整应用程序窗口的大小。笔画未渲染到 RenderTargetBitmap。图像仅显示浅蓝色矩形。

有人对此有解决方案吗?

**更新**

对于那些正在寻找将笔画保存到 uwp 上的位图的正确方法的人。我找到了将 InkCanvas 笔画保存到位图的 UWP 方式:InkStrokeContainer 对象有一个名为 SaveAsync(...) 的方法,该方法将笔画保存到流中。如果您将此流用作位图的源,您将获得作为位图的笔划。

        InkStrokeContainer container = TheInkCanvas.InkPresenter.StrokeContainer;
WriteableBitmap bmp;
using (InMemoryRandomAccessStream ims =
new InMemoryRandomAccessStream())
{
await container.SaveAsync(ims);
bmp = await new WriteableBitmap(1, 1)
.FromStream(ims, BitmapPixelFormat.Bgra8);
}

*注意:此示例代码中使用了 WriteableBitmapEx ( https://www.nuget.org/packages/WriteableBitmapEx/ )

最佳答案

您可以使用 Win2D渲染墨迹描边。

  1. 从 NuGet 将 Win2D.uwp 添加到项目中
  2. 将此 InkCanvasExtensions.cs 文件添加到您的项目
  3. 更改命名空间或将 using Zoetrope; 添加到您的源
  4. 创建图像文件或流并将其传递给 InkCanvas.RenderAsync()

InkCanvasExtensions.cs

namespace Zoetrope
{
using System;
using System.Threading.Tasks;
using Microsoft.Graphics.Canvas;
using Windows.Graphics.Display;
using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Xaml.Controls;

/// <summary>
/// InkCanvas Extensions
/// </summary>
public static class InkCanvasExtensions
{
/// <summary>
/// Render an InkCanvas to a stream
/// </summary>
/// <param name="inkCanvas">the ink canvas</param>
/// <param name="fileStream">the file stream</param>
/// <param name="backgroundColor">the background color</param>
/// <param name="fileFormat">the file format</param>
/// <returns>an async task</returns>
public static async Task RenderAsync(
this InkCanvas inkCanvas,
IRandomAccessStream fileStream,
Color backgroundColor,
CanvasBitmapFileFormat fileFormat)
{
// do not put in using{} structure - it will close the shared device.
var device = CanvasDevice.GetSharedDevice();
var width = (float) inkCanvas.ActualWidth;
var height = (float) inkCanvas.ActualHeight;
var currentDpi = DisplayInformation.GetForCurrentView().LogicalDpi;

using (var offscreen = new CanvasRenderTarget(device, width, height, currentDpi))
{
using (var session = offscreen.CreateDrawingSession())
{
session.Clear(backgroundColor);

session.DrawInk(inkCanvas.InkPresenter.StrokeContainer.GetStrokes());
}

await offscreen.SaveAsync(fileStream, fileFormat);
}
}
}
}

关于xaml - 通用 Windows InkCanvas 笔触在 RenderTargetBitmap.RenderAsync 上消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30621375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com