Texture2D.GetData Generic Method (Int32, Nullable<Rectangle>, T , Int32, Int32)
Gets a copy of 2D texture data, specifying a mipmap level, source rectangle, start index, and number of elements.
Syntax
'Declaration
Public Sub GetData(Of T As ValueType) ( _
level As Integer, _
rect As Nullable(Of Rectangle), _
data As T(), _
startIndex As Integer, _
elementCount As Integer _
)
public void GetData<T> (
int level,
Nullable<Rectangle> rect,
T[] data,
int startIndex,
int elementCount
) where T : ValueType
public:
generic<typename T> where T : ValueType
void GetData(
int level,
Nullable<Rectangle> rect,
T data[],
int startIndex,
int elementCount
)
Type Parameters
- T
The type of the elements in the array. This is usually a Microsoft.Xna.Framework.Graphics.PackedVector type but it can be any fundamental type that matches the bit size of the surface format.
Parameters
- level
Type: Int32
Mipmap level. - rect
Type: Nullable<Rectangle>
The section of the texture to copy. null indicates the data will be copied from the entire texture. - data
Type: T
Array of data. - startIndex
Type: Int32
Index of the first element to get. - elementCount
Type: Int32
Number of elements to get.
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. |
Example
In this example, the color of the pixel beneath the mouse is retrieved from the back buffer.
sourceTexture.SetData<Color>(sourceColorData);
// Initialize a texture with texture data from another texture
sourceTexture.GetData<Color>(destinationColorData);
destinationTexture.SetData<Color>(destinationColorData);
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 <
GraphicsDevice.PresentationParameters.BackBufferWidth) &&
(Mouse.GetState().Y <
GraphicsDevice.PresentationParameters.BackBufferHeight))
{
backBufferData = new Texture2D(
GraphicsDevice,
GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight);
Rectangle sourceRectangle =
new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);
Color[] retrievedColor = new Color[1];
backBufferData.GetData<Color>(
0,
sourceRectangle,
retrievedColor,
0,
1);
}
Requirements
Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework.Graphics (in microsoft.xna.framework.graphics.dll)
See Also
Reference
Texture2D Class
Texture2D Members
Microsoft.Xna.Framework.Graphics Namespace
Platforms
Windows Phone