2012-04-18 16 views
1

我在代碼中創建的Viewport3D:WPF,C#的Viewport3D的renderTargetBitmap沒有將其分配給一個窗口

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

回答

0

問題解決了!我無法回答我的問題 - 不知道爲什麼...我添加了以下序列:

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)); 
相關問題