试图在XNA中截取Windows Phone的屏幕截图

时间:2014-02-26 13:54:56

标签: c# vb.net windows-phone-7 windows-phone-8 xna

我目前正在使用一段代码尝试在XNA中截取当前屏幕的截图。我已经用VB.NET编写了代码。这是:

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

但是,虽然此代码将图片保存到我保存的图片相册,但它只是显示为紫色屏幕。谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:1)

我在几年前的一个游戏中做到了这一点。示例在C#中,但它应该很容易翻译:

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;

此时,您应该能够以与当前使用“screenshottexture”变量相同/非常相似的方式使用Texture2d(屏幕截图)。

编辑 - 没有意识到我在上面的代码中引用了CreateScreenshot()方法:

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

        Draw(spriteBatch);

        spriteBatch.End();

        return spriteBatch;
    }