如何解决 WPF 复合渲染线程上 Helix Toolkit 渲染的同步问题 [英] How to resolve synchronisation problem with Helix Toolkit rendering on WPF composite render thread

查看:99
本文介绍了如何解决 WPF 复合渲染线程上 Helix Toolkit 渲染的同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#、WPF、Helix 工具包.我正在尝试从 HelixViewport3D 生成位图,但遇到了一些问题.

C#, WPF, Helix Toolkit. I am trying to generate a bitmap from a HelixViewport3D and have encountered a few problems.

第一个问题是我找不到在屏幕外渲染的方法.网上有一些对此的引用(例如此处)和作为据我所知,它没有内置解决方案.

The first problem is that I cannot find a way to render off-screen. There are a few references to this online (e.g. here) and as far as I can see it does not have a built-in solution.

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

As a somewhat sub-optimal workaround I have proceeded to render to the screen where the user can see it, with the intention of creating a bitmap from that rendered image. I now have the problem that an image exported from the content on screen (e.g. using Viewport3DHelper.SaveBitmap) is blank if I call it immediately. I understand that this is because Helix Toolkit is rendering the image in the WPF composite render thread, so there is no image to grab at the time I try to grab it, because it has not yet been rendered.

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

I am not aware of a 'render complete' event that I can subscribe to. Is there one?

如果没有,是否有一种解决方法可能是使用线程优先级来降低我的代码的优先级,以便它在继续之前等待渲染完成?

If not, would a workaround perhaps be to use thread priorities in order to make my code lower priority, so that it waits for the rendering to complete before continuing?

<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.

The Action will be called once the rendering is done.

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

So in your case you could use it like this:

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);
}

这篇关于如何解决 WPF 复合渲染线程上 Helix Toolkit 渲染的同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆