wpf, c#, renderTargetBitmap of viewport3D 而不将其分配给窗口

本文关键字:分配 窗口 renderTargetBitmap of viewport3D wpf | 更新日期: 2023-09-27 18:31:33

我在代码中创建了一个Viewport3D:

Viewport3D CurViewport3D = new Viewport3D();
CurViewport3D = CreateViewportWithContent(); //fill the viewport with some content
BitmapSource bmp;
var renderTargetBitmap = new RenderTargetBitmap(600, 300, 96, 96, PixelFormats.Default);
renderTargetBitmap.Render(CurViewport3D);
bmp = (BitmapSource)renderTargetBitmap;
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmp));
using (Stream stm = File.Create("outp.png")){ png.Save(stm);}

但不幸的是,outp.png是空的,没有任何内容。如果我将视口应用于窗口:

MainGrid.Children.Add(CurViewport3D); //MainGrid is part of the window

一切都很完美。输出.png不是空的。有没有人知道如何在不将视口应用于窗口的情况下获得正确的图像?

问题解决了!我无法回答我的问题 - 不知道为什么...我添加了以下序列:

int width = 500;
int height = 300;
CurViewport3D.Width = width;
CurViewport3D.Height = height;
CurViewport3D.Measure(new Size(width, height));
CurViewport3D.Arrange(new Rect(0, 0, width, height));

wpf, c#, renderTargetBitmap of viewport3D 而不将其分配给窗口

问题解决了!我无法回答我的问题 - 不知道为什么...我添加了以下序列:

int width = 500;
int height = 300;
CurViewport3D.Width = width;
CurViewport3D.Height = height;
CurViewport3D.Measure(new Size(width, height));
CurViewport3D.Arrange(new Rect(0, 0, width, height));