Creating a Render Target
Demonstrates how to create a render target using the RenderTarget2D class.
The Complete Sample
The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Creating a RenderTarget
To create a render target
Declare variables for a render target using the RenderTarget2D class and the render target texture using a Texture2D class.
SpriteBatch spriteBatch; Texture2D grid; RenderTarget2D renderTarget;
Create a render target, giving it the same size as the back buffer.
renderTarget = new RenderTarget2D( GraphicsDevice, GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);
Load a grid for a texture, which contains two vertical and two horizontal lines.
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); grid = Content.Load<Texture2D>("grid"); }
Render the texture to the render target.
This function sets the render target on the device, draws the texture (to the render target) using a SpriteBatch, and sets the device render target to null (which resets the device to the back buffer).
private void DrawRenderTarget() { // Set the device to the render target graphicsDeviceManager.GraphicsDevice.SetRenderTarget(renderTarget); graphicsDeviceManager.GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); Vector2 pos = Vector2.Zero; spriteBatch.Draw(grid, pos, Color.White); spriteBatch.End(); // Reset the device to the back buffer graphicsDeviceManager.GraphicsDevice.SetRenderTarget(null); }
Draw the render target texture to the back buffer.
graphicsDeviceManager.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw((Texture2D)renderTarget, new Vector2(200, 50), // x,y position new Rectangle(0, 0, 32, 32), // just one grid Color.White // no color scaling ); spriteBatch.End();