繁体   English   中英

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

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

我用代码创建一个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));

暂无
暂无

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

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