Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wpf、c#、renderTargetBitmap的viewport3D,而不将其指定给窗口_C#_.net_Wpf - Fatal编程技术网

wpf、c#、renderTargetBitmap的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:

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