简体   繁体   中英

Trying to take a screenshot of Windows Phone in XNA

I am currently using a piece of code to try and take a screenshot of my current screen in XNA. I have written the code in VB.NET. This is:

    Public Sub SaveScore()
    Dim screenshottexture As RenderTarget2D = New RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, False, SurfaceFormat.Color, Nothing)
    GraphicsDevice.SetRenderTarget(screenshottexture)
    GraphicsDevice.SetRenderTarget(Nothing)
    Using stream As New MemoryStream()
        screenshottexture.SaveAsJpeg(stream, screenshottexture.Width, screenshottexture.Height)
        stream.Position = 0
        Dim media As New MediaLibrary()
        media.SavePicture("screenshot.jpg", stream)
    End Using
    screenshottaken = True
    screenshottexture.Dispose()

However, although this code saves a picture to my saved pictures album, it just appears as a purple screen. Can anybody see what i am doing wrong?

I did this in a game I was working on a couple of years ago. Example is in C#, but it should be easily translatable:

Texture2D screenshot;
RenderTarget2D render;
SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice);
//Game1.graphics.GraphicsDevice.Clear(Color.Black);

render = new RenderTarget2D(Game1.graphics.GraphicsDevice, 800, 480);

Game1.graphics.GraphicsDevice.SetRenderTarget(render);

spriteBatch = CreateScreenshot(spriteBatch);

Game1.graphics.GraphicsDevice.SetRenderTarget(null);

screenshot = render as Texture2D;

At that point, you should be able to use the Texture2d(screenshot) in the same/very similar way you are currently using your "screenshottexture" variable.

EDIT - Didn't realize I was references the CreateScreenshot() method in the above code:

    public SpriteBatch CreateScreenshot(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();

        Draw(spriteBatch);

        spriteBatch.End();

        return spriteBatch;
    }

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