wpf、c#、renderTargetBitmap的viewport3D,而不将其指定给窗口
我在代码中创建了一个Viewport3D:wpf、c#、renderTargetBitmap的viewport3D,而不将其指定给窗口,c#,.net,wpf,C#,.net,Wpf,我在代码中创建了一个Viewport3D: Viewport3D CurViewport3D = new Viewport3D(); CurViewport3D = CreateViewportWithContent(); //fill the viewport with some content BitmapSource bmp; var renderTargetBitmap = new RenderTargetBitmap(600, 300, 96, 96, PixelFormats.Def
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
一切都很完美。outp.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));
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));
问题解决了!我不能回答我的问题-不知道为什么。。。我添加了以下序列:
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));
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));