How to get a 2d bitmap of 3d control

Nov 26, 2013 at 1:57 PM
Hi,

Im looking to take a snap shot of whats in the helix HelixViewport3D control by clicking a button. Has anyone done this before? Any advice appreciated!

Thanks!
Nov 27, 2013 at 2:41 PM
Edited Nov 27, 2013 at 2:42 PM
something like this should do the work (helix is your HelixViewport3D):


Bitmap RenderBitmapAndSaveToFile(int width, string fileName)
{
double scale = width / helix.ActualWidth;

int height = (int)(helix.ActualHeight * scale);

RenderTargetBitmap bmp = new RenderTargetBitmap( width, height, 96 * scale, 96 * scale, PixelFormats.Pbgra32);

// draw the bitmap Background
Rectangle vRect = new Rectangle();
vRect.Width = bmp.Width;
vRect.Height = bmp.Height;
vRect.Fill = helix.Background;
vRect.Arrange(new Rect(0, 0, vRect.Width, vRect.Height));
bmp.Render(vRect);

bmp.Render(helix.Viewport);

PngBitmapEncoder aEncoder = new PngBitmapEncoder();
aEncoder.Frames.Add(BitmapFrame.Create(bmp));

Directory.CreateDirectory(System.IO.Path.GetDirectoryName(fileName));

using (Stream stm = File.Create(fileName))
{
aEncoder.Save(stm);
}
}

regads,
Joachim