Sdílet prostřednictvím


Texture2D.GetData Generic Method (T , Int32, Int32)

Copies texture data into an array.

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

Syntax

public void GetData<T> (
         T[] data,
         int startIndex,
         int elementCount
) where T : ValueType

Type Parameters

Parameters

  • data
    The array to receive texture data.
  • startIndex
    The index of the element in the array at which to start copying.
  • elementCount
    The number of elements to copy.

Remarks

On Windows, GetData and SetData will fail if Texture2D.ResourceManagementMode is ResourceManagementMode.Manual and the format cannot be used as a render target.

Exceptions

Exception type Condition
ArgumentNullException data must be of sufficient length to receive the data.
InvalidOperationException The vertex stride is larger than the vertex buffer, or the vertex stride is too small for the type of data requested.
NotSupportedException This Texture2D was created with a usage type of ResourceUsage.WriteOnly. Calling GetData on a resource that was created with ResourceUsage.WriteOnly is not supported.

Example

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

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 Texture2D( 
        graphics.GraphicsDevice,
        graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
        graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
        1,
        ResourceUsage.ResolveTarget,
        graphics.GraphicsDevice.PresentationParameters.BackBufferFormat,
        ResourceManagementMode.Manual );

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

}

See Also

Reference

Texture2D Class
Texture2D Members
Microsoft.Xna.Framework.Graphics Namespace

Platforms

Xbox 360, Windows XP SP2, Windows Vista