2011-05-19 18 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

相關問題