c# - 在控制台应用程序中以编程方式动态创建和导出 WPF Viewport3D 视觉对象

标签 c# wpf wpf-controls console-application

我正在使用 Viewport3D 的 wpf 用户控件库中开发 wpf 用户控件。

我在运行应用程序时在视口(viewport)中看到图像,如果我在 WPF 应用程序 中调用它,我可以使用 RenderTargetBitmap 将它保存到文件中。

但是,如果我在控制台应用程序 中实例化用户控件并尝试导出空图像,则会保存。代码是

class Program
{
    static void Main(string[] args)
    {
        Thread t = new Thread(DoIt);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        Console.WriteLine("press enter");
        Console.ReadLine();
    }
    static void DoIt()
    {
        var x = new WpfControlLibrary1.Showcase();

        x.GenerateViewPort();
        x.TakeScreenshot(300, 300);
    }
}

public partial class Showcase : UserControl
{
    private PerspectiveCamera _PerspectiveCamera = null;

    public Showcase()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler((sender, e) => this.GenerateViewPort());
    }

    public void GenerateViewPort()
    {
        this._PerspectiveCamera = new PerspectiveCamera(new Point3D(0, 0, 5), new Vector3D(0, 0, -5), new Vector3D(0, 1, 0), 45);

        var viewport3D = new Viewport3D() { Camera = this._PerspectiveCamera };
        viewport3D.Children.Add(new ModelVisual3D() { Content = GenerateModel() });
        viewport3D.Loaded += new RoutedEventHandler(this.viewport3D_Loaded);
        this.grdAniContainer.Children.Add(viewport3D);
    }

    private static Model3DGroup GenerateModel()
    {
        ...
    }

    void viewport3D_Loaded(object sender, RoutedEventArgs e)
    {
        ...
    }

    public void TakeScreenshot(int width, int height)
    {
        takeScreenshot(this.grdAniContainer.Children[0], width, height);
    }

    private static void takeScreenshot(Visual visual, int width, int height)
    {
        var v = visual as Viewport3D;
        v.Width = width;
        v.Height = height;
        v.Measure(new Size(width, height));
        v.Arrange(new Rect(0, 0, width, height));


        var vRect = new Rectangle();
        vRect.Width = width;
        vRect.Height = height;
        vRect.Fill = Brushes.White;
        vRect.Arrange(new Rect(0, 0, vRect.Width, vRect.Height));

        var bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
        bmp.Render(vRect);
        bmp.Render(v);

        var png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(bmp));

        var dlg = new SaveFileDialog();
        dlg.DefaultExt = ".png";
        dlg.Filter = "PNG Images (.png)|*.png";
        if (dlg.ShowDialog() ?? false == true)
        {
            string filepath = dlg.FileName;
            using (var stm = File.Create(filepath))
                png.Save(stm);
        }
    }

}

Load 事件未触发,viewport3D_Loaded 未在控制台应用程序中处理,因此我手动调用。

有没有一种方法可以将视觉效果加载到内存中并以编程方式呈现到文件中?

最佳答案

我需要在“takeScreenshot”方法中实例化“bmp”之后添加这个委托(delegate)

v.Dispatcher.Invoke(((Action)(() => bmp.Render(v))), DispatcherPriority.Render);

关于c# - 在控制台应用程序中以编程方式动态创建和导出 WPF Viewport3D 视觉对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16640938/

相关文章:

WPF TabControl 和 DataTemplates

c# - WPF 线程延迟

wpf - 在 WPF 中将数据表与 ComboBox 绑定(bind)

c# - 如何在 WPF 3D 中使用顶点处的值对网格着色?

c# - 仅在 Azure 应用服务中出现错误 : Object reference not set to an instance of an object

c# - 我无法在 umbraco 中发送邮件

wpf - Items控制项,走出视野

c# - 使用 agility pack C# 处理 cookie 和 header

c# - WPF 应用程序设置 - 重置单个属性

.net - 如何在自定义 WPF 文本框中显示插入符号