【问题标题】:Why are my screenshots only black?为什么我的截图只有黑色?
【发布时间】:2011-05-20 18:10:15
【问题描述】:

谁能告诉我为什么我的截图只有黑色?我还在学习,找不到线索为什么它们只是黑色的。

这是我的实用程序类

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);
    }

【问题讨论】:

    标签: screenshot xna-4.0


    【解决方案1】:

    您实际上并没有渲染到您的渲染目标。所以你正在保存空白目标。

    你需要像这样包装你的场景图:

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

    【讨论】:

    • 我不明白...对不起。我必须将此代码放入或与女巫行一起放入其中,我必须更改它..我现在很困惑...大声笑
    • @Dave:在创建渲染目标之后,保存它之前,你必须向它绘制一些东西。为了绘制到渲染目标,必须在设备上进行设置(因此您的场景绘制到渲染目标而不是后台缓冲区)。
    • 创建渲染目标后 --> GraphicsDevice.SetRenderTarget(ssTexture);在我保存之前 -->> ssTexture.SaveAsPng(fileStream, pparameters.BackBufferWidth, pparameters.BackBufferHeight);你必须给它画点东西。我想我在用这个做些什么 -->ssTexture = new RenderTarget2D(device,pparameters.BackBufferWidth, pparameters.BackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None);还是我傻??我真的不明白。
    • 我只有一个 XNA 3.1 导师,我自己解决了所有这些问题,但我还是卡住了。在 3.1 中使用了 ResolveTexture2D,在 4.0 中已经过时了。
    • 你需要渲染你的场景——就像你在游戏的Draw 函数中所做的那样——渲染目标(我已经在其中放置了一条评论,指示你这样做)。到目前为止,您或我发布的代码都没有这样做。带有new 的行是创建 空渲染目标的内容。 SetRenderTarget 告诉图形设备将其用作绘图表面。在 XNA 4.0 中,渲染目标也是一个纹理 - 您不需要解析它,只需调用 SaveAsPng 即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多