如何解决在WPF复合渲染线程上使用Helix Toolkit渲染的同步问题



C#、WPF、Helix Toolkit。我正在尝试从HelixViewport3D生成位图,遇到了一些问题。

第一个问题是我找不到一种在屏幕外渲染的方法。网上有一些关于这一点的参考资料(例如这里(,据我所见,它没有内置的解决方案。

作为一种有点次优的解决方法,我已经开始渲染到用户可以看到的屏幕上,目的是从渲染的图像创建位图。我现在遇到的问题是,如果我立即调用,从屏幕上的内容导出的图像(例如使用Viewport3DHelper.SaveBitmap(是空白的。我知道这是因为Helix Toolkit正在WPF复合渲染线程中渲染图像,所以在我尝试获取图像时没有图像可获取,因为它尚未渲染。

我不知道我可以订阅"渲染完成"事件。有吗?

如果没有,是否可以使用线程优先级来降低代码的优先级,以便在继续之前等待渲染完成?

<Window x:Class=".MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test"
xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf" xmlns:h="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid>
<h:HelixViewport3D x:Name="helixPlot" Width="450" Height="450"/>
</Grid>
<StackPanel Orientation="Horizontal">
<Button Name ="btnGo" Height="25" Content="Render" VerticalAlignment="Bottom" Click="Go_Click"/>
<Button Name ="btnTemp" Height="25" Content="Write PNG" VerticalAlignment="Bottom" Click="Test_Click"/>
</StackPanel>
</Grid>
</Window>

using System.Collections.Generic;
using System.Windows;
using HelixToolkit.Wpf;
using System.Windows.Media.Media3D;
using System.Windows.Media;
namespace Test
{
class Foo
{
public List<Point3D> points;
public Foo()
{ // constructor creates three arbitrary 3D points
points = new List<Point3D>() { new Point3D(0, 0, 0), new Point3D(1, 0, 0), new Point3D(0, 0, 1) };
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void renderImages()
{
Foo bar = new Foo(); // create object with three 3D points
DrawStuff(bar.points); // plot to helixViewport3D control ('points' = list of 3D points)
helixPlot.CameraController.ZoomExtents();
// This results in a blank image because image not yet rendered...
Viewport3DHelper.SaveBitmap(helixPlot.Viewport, @"E:test.png", null, 4, BitmapExporter.OutputFormat.Png);

}
private void DrawStuff(List<Point3D> points)
{

Point3DCollection dataList = new Point3DCollection();
PointsVisual3D cloudPoints = new PointsVisual3D { Color = Colors.Red, Size = 5.0f };
foreach (Point3D p in points)
{
dataList.Add(p);
}
cloudPoints.Points = dataList;
// Add geometry to helixPlot. It renders asynchronously in the WPF composite render thread...
helixPlot.Children.Add(cloudPoints);
}
// When this is clicked we render image and (try to) save to file...
private void Go_Click(object sender, RoutedEventArgs e)
{
renderImages();
}
// To demonstrate that the image export is not the problem.
// This works if the image has been rendered already...
private void Test_Click(object sender, RoutedEventArgs e)
{
Viewport3DHelper.SaveBitmap(helixPlot.Viewport, @"E:test.png", null, 4, BitmapExporter.OutputFormat.Png);
}
}
}

由于这个答案,我设法找到了一个解决方案:

Dispatcher.BeginInvoke(new Action(() => DoSomething()), DispatcherPriority.ContextIdle, null);

渲染完成后将调用Action。

所以在你的情况下,你可以这样使用它:

private void renderImages()
{
Foo bar = new Foo(); // create object with three 3D points
this.DrawStuff(bar.points, SaveHelixPlotAsBitmap /*the action you want to perform once the rendering is done*/);    
}
private void DrawStuff(List<Point3D> points, Action renderingCompleted)
{
//Draw everyting you need ...
Dispatcher.BeginInvoke(renderingCompleted, DispatcherPriority.ContextIdle);
}
private void SaveHelixPlotAsBitmap()
{
helixPlot.CameraController.ZoomExtents();
Viewport3DHelper.SaveBitmap(helixPlot.Viewport, @"E:test.png", null, 4, BitmapExporter.OutputFormat.Png);
}

最新更新