0

How to get a 2d bitmap of 3d control

Anonymous 10 years ago 0
This discussion was imported from CodePlex

mcab21 wrote at 2013-11-26 13:57:

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!

tbd2 wrote at 2013-11-27 14:41:

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