【问题标题】:Trying to take a screenshot of Windows Phone in XNA尝试在 XNA 中截取 Windows Phone 的屏幕截图
【发布时间】:2014-02-26 13:54:55
【问题描述】:

我目前正在使用一段代码尝试在 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()

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

【问题讨论】:

  • 在将rendertarget 设置为nothing 之前尝试强制游戏进行绘制。

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


【解决方案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(screenshot)。

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

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

        Draw(spriteBatch);

        spriteBatch.End();

        return spriteBatch;
    }

【讨论】:

  • 所以一旦完成,我就会通过我的流来保存它?
  • “CreateScreenshot”也是某种类吗?
  • 没有意识到我在原始代码中引用了这个方法。我将其添加到主要答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
相关资源
最近更新 更多