简体   繁体   中英

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

I create a Viewport3D in code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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