Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a screenshot with silverlight 5 / xna

I'm trying to take a screenshot of a subclassed XNA DrawingSurface element from within a silverlight 5 application. This sl app will run inside an aspx page.

Here's what I've tried so far without success:

WriteableBitmap bmp = new WriteableBitmap(LayoutRoot, null); 
testImage.Source = bmp; 

with LayoutRoot being the parent control on the silverlight page and testImage is just an Image control on the page to see if the screenshot is correct.

This will render all the silverlight controls on the page just fine, but the drawingsurface part remains empty. In other words the XNA content is not rendered into the image.

I've also tried to render the XNA content to a RenderTarget2D, but the silverlight version of the render target does not seem to have any methods to save the data. I saw some WP7 examples that used a method called SaveAsJpeg(), but that doesn't seem to be available in the SL5 version of the class.

I would appreciate any help with this.

Greets,

Floris

like image 279
Floris Groen Avatar asked Mar 30 '12 07:03

Floris Groen


1 Answers

One possibility would be to draw your screen into a RenderTarget2D and then use the method SaveAsPng like:

using (Stream stream = File.OpenWrite("filename.png"))
{
renderTarget2D.SaveAsPng(stream, renderTarget2D.Width, renderTarget2D.Height);
}
like image 115
ares_games Avatar answered Nov 15 '22 08:11

ares_games