在 UWP 中显示许多迷你版绘制的 InkCanvas 的最佳方式

Best way to show many mini versions of painted InkCanvas in UWP

我在描绘我的 InkCanvas 的许多迷你版本时遇到了问题。在我的应用程序中,可以在 InkCanvas 上书写或绘图。现在我想在 GridView 中描绘所有创建的 InkCanvas。

但是对于这个 GridView 中的迷你版本,我无法创建足够的迷你版本。

我用 36 个迷你版本对其进行了测试,在我显示一个迷你版本并返回后,应用程序每次都会崩溃 通过渲染相同的迷你 InkCanvas 并出现错误:内存不足。 所以我搜索并找到这个 post:

Insufficient memory to continue the execution of the program when trying to initialize array of InkCanvas in UWP

我检查了内存工作负载:

     var AppUsageLevel = MemoryManager.AppMemoryUsageLevel;
     var AppMemoryLimit = MemoryManager.AppMemoryUsageLimit;

并且内存有足够的空闲 space。 (这是一个错误吗?)

所以我尝试用 InkCanvas 从我的网格中渲染图像,但笔画没有渲染,所有图片都是空的。我可以节省内存吗这条路?)

所以现在我的问题是:

谁能告诉我如何解决这个问题?最好的方法是什么?

非常感谢您!

阿格雷多

如果您想预览您的绘图,更好的方法是将它们渲染为位图并在网格中显示此位图,而不是 InkCanvas 的多个复杂控件。

下面是一些代码,用于将 中的墨水渲染为位图:

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(inkCanvas.InkPresenter.StrokeContainer.GetStrokes());
}

using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
    await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);

您还需要将 Win2D.uwp nuget 包添加到您的项目中。