为什么我的截图只是黑色?

时间:2011-05-19 09:53:50

标签: screenshot xna-4.0

有人可以告诉我为什么我的截图只是黑色的吗?我还在学习,无法找到为什么他们只是黑人的线索。

这是我的实用工具类

static class XNAUtilities
{
    private static RenderTarget2D ssTexture;
    private static KeyboardState currentState, previousState;
    private static int counter;

    public static void TakeScreenShot(GraphicsDevice device, Keys theKey)
    {
        // Take Screenshot
        currentState = Keyboard.GetState();

        if (currentState.IsKeyDown(theKey) && previousState.IsKeyUp(theKey))
        {
            //device.SetRenderTarget(null);                                                         
            PresentationParameters pparameters = device.PresentationParameters;
            ssTexture = new RenderTarget2D(device, pparameters.BackBufferWidth, pparameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None); //??
            FileStream fileStream = new System.IO.FileStream(@"Screenshot" + "_" + counter + ".png", System.IO.FileMode.CreateNew);
            ssTexture.SaveAsPng(fileStream, pparameters.BackBufferWidth, pparameters.BackBufferHeight);

            counter++;
        }
        previousState = currentState;
    }
}

}

这是我从Game1.cs的更新和绘制

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        myModelRotation += MathHelper.ToRadians(1f);

        // Take a Screenshot
        XNAUtilities.TakeScreenShot(GraphicsDevice, Keys.F8);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        Matrix[] transforms = new Matrix[myModel.Bones.Count];
        myModel.CopyAbsoluteBoneTransformsTo(transforms);

        foreach (ModelMesh mesh in myModel.Meshes)
        {
            foreach (BasicEffect effects in mesh.Effects)
            {
                effects.EnableDefaultLighting();
                effects.World = transforms[mesh.ParentBone.Index]
                    * Matrix.CreateRotationY(myModelRotation)
                    * Matrix.CreateTranslation(myModelPosition);
                effects.View = Matrix.CreateLookAt(new Vector3(200, 100, 400), Vector3.Zero, Vector3.Up);

                effects.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
                    GraphicsDevice.Viewport.AspectRatio, 1, 5000);
            }
            mesh.Draw();
        }


        smileySprite.DrawSprites(GraphicsDevice, spriteBatch, new Vector2(10,10), Color.White);

        base.Draw(gameTime);
    }

1 个答案:

答案 0 :(得分:1)

您实际上并未渲染到渲染目标。所以你要保存空白目标。

您需要像这样包装场景图:

GraphicsDevice.SetRenderTarget(ssTexture);
// Render your scene here
GraphicsDevice.SetRenderTarget(null);
// Now you can save your render target as a texture