Click to Rate and Give Feedback
MSDN
MSDN Library
XNA Game Studio
XNA Game Studio 2.0
Texture2D Class
Texture2D Methods
GetData Method
 GetData Generic Method (T[])
XNA Game Studio 2.0
Texture2D.GetData Generic Method (T[])
Copies texture data into an array.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

C#
public void GetData<T> (
         T[] data
) where T : ValueType

Type Parameters

T
The type of the elements in the array. This is usually a Microsoft.Xna.Framework.Graphics.PackedVector type that matches the Format of the texture, but it can be any fundamental type that matches the bit size of the surface format.

Parameters

data
The array to receive texture data.
Exception typeCondition
ArgumentNullException data must be of sufficient length to receive the data.
InvalidOperationExceptionThe vertex stride is larger than the vertex buffer, or the vertex stride is too small for the type of data requested.

In this example, the color of the pixel beneath the mouse is retrieved from the back buffer.

C#
if (Mouse.GetState().LeftButton == ButtonState.Pressed &&   // If the left button is pressed
    Mouse.GetState().X > 0 && Mouse.GetState().Y > 0 &&     // and we are inside the game window
    Mouse.GetState().X < graphics.GraphicsDevice.PresentationParameters.BackBufferWidth &&
    Mouse.GetState().Y < graphics.GraphicsDevice.PresentationParameters.BackBufferHeight)
{
    backBufferData = new ResolveTexture2D(
        graphics.GraphicsDevice,
        graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
        graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
        1,
        graphics.GraphicsDevice.PresentationParameters.BackBufferFormat);


    Rectangle sourceRectangle =
        new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);

    Color[] retrievedColor = new Color[1];

    graphics.GraphicsDevice.ResolveBackBuffer(backBufferData);

    backBufferData.GetData<Color>(
        0,
        sourceRectangle,
        retrievedColor,
        0,
        1);
}

Xbox 360, Windows XP SP2, Windows Vista

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker