简体   繁体   English

WPF,C#,viewport3D的renderTargetBitmap而不将其分配给窗口

[英]wpf, c#, renderTargetBitmap of viewport3D without assigning it to a window

I create a Viewport3D in code: 我用代码创建一个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);}

But unfortunately the outp.png is empty without any content. 但是不幸的是outp.png是空的,没有任何内容。 In case I'm applying the viewport to a window: 如果我将视口应用于窗口:

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

Everything works perfect. 一切正常。 The outp.png is not empty. outp.png不为空。 Does anyone have any idea what to do to get the correct image without applying the viewport to a window? 没有人知道如何在不将视口应用于窗口的情况下获取正确的图像吗?

Problem solved! 问题解决了! I can not answer my question - don't know why ... I added the following sequence: 我无法回答我的问题-不知道为什么...我添加了以下顺序:

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

Problem solved! 问题解决了! I can not answer my question - don't know why ... I added the following sequence: 我无法回答我的问题-不知道为什么...我添加了以下顺序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM