2011-05-19 69 views
2

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

这是我的实用工具类

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

你没有真正呈现到您的渲染目标的平局。所以你要保存空白的目标。

你需要用你的场景画,像这样:

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

我不明白它... SRY。在那里我必须把这段代码或与女巫线我必须改变它..我现在感到困惑...大声笑 – Dave 2011-05-19 11:06:52

+0

@Dave:在你创建渲染目标,*之前*你保存它,你必须*画一些东西*。为了绘制到渲染目标,它必须在设备上设置(所以你的场景绘制到渲染目标而不是后台缓冲区)。 – 2011-05-19 11:17:44

+0

创建渲染目标后 - > GraphicsDevice.SetRenderTarget(ssTexture); 和之前我保存它 - >> ssTexture.SaveAsPng(fileStream,pparameters.BackBufferWidth,pparameters.BackBufferHeight); 你必须画一些东西给它。 我想我是这样做的 - > ssTexture = new RenderTarget2D(device,pparameters.BackBufferWidth,pparameters.BackBufferHeight,false,SurfaceFormat.Color,DepthFormat.None); 还是我很笨? 我真的不知道它。 – Dave 2011-05-19 11:26:14

相关问题