Class Texture2D
Represents a 2D grid of texels.
public class Texture2D : Texture, IDisposable
- Inheritance
-
Texture2D
- Implements
- Derived
- Inherited Members
Remarks
A texel represents the smallest unit of a texture that can be read from or written to by the GPU. A texel is composed of 1 to 4 components. Specifically, a texel may be any one of the available texture formats represented in the SurfaceFormat enumeration.
Constructors
Texture2D(GraphicsDevice, int, int)
Creates an uninitialized Texture2D resource with the specified parameters.
public Texture2D(GraphicsDevice graphicsDevice, int width, int height)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device used to display the texture.
widthintThe width, in pixels, of the texture.
heightintThe height, in pixels, of the texture.
Remarks
To initialize the texture with data after creating, you can use one of the SetData method.
To initialize a Texture2D from an existing file use the ContentManager.Load method if loading a pipeline preprocessed Texture2D from an .xnb file or Texture2D.FromFile to load directly from a file.
Exceptions
- ArgumentNullException
The
graphicsDeviceparameter is null.- ArgumentOutOfRangeException
The
widthand/orheightless than or equal to zero.
Texture2D(GraphicsDevice, int, int, bool, SurfaceFormat)
Creates an uninitialized Texture2D resource with the specified parameters.
public Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device used to display the texture.
widthintThe width, in pixels, of the texture.
heightintThe height, in pixels, of the texture.
mipmapbooltrue if mimapping is enabled for the texture; otherwise, false.
formatSurfaceFormatThe surface format of the texture.
Remarks
To initialize the texture with data after creating, you can use one of the SetData method.
To initialize a Texture2D from an existing file use the ContentManager.Load method if loading a pipeline preprocessed Texture2D from an .xnb file or Texture2D.FromFile to load directly from a file.
Exceptions
- ArgumentNullException
The
graphicsDeviceparameter is null.- ArgumentOutOfRangeException
The
widthand/orheightless than or equal to zero.
Texture2D(GraphicsDevice, int, int, bool, SurfaceFormat, SurfaceType, bool, int)
Creates an uninitialized Texture2D resource with the specified parameters.
protected Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format, Texture2D.SurfaceType type, bool shared, int arraySize)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device used to display the texture.
widthintThe width, in pixels, of the texture.
heightintThe height, in pixels, of the texture.
mipmapbooltrue if mimapping is enabled for the texture; otherwise, false.
formatSurfaceFormatThe surface format of the texture.
typeTexture2D.SurfaceTypeThe surface type of the texture
sharedboolWhether this render target resource should be a shared resource accessible on another device. This property is only valid for DirectX targets.
arraySizeintThe size of the texture array.
Remarks
To initialize the texture with data after creating, you can use one of the SetData method.
To initialize a Texture2D from an existing file use the ContentManager.Load method if loading a pipeline preprocessed Texture2D from an .xnb file or Texture2D.FromFile to load directly from a file.
Exceptions
- ArgumentNullException
The
graphicsDeviceparameter is null.- ArgumentOutOfRangeException
The
widthand/orheightless than or equal to zero.- ArgumentException
The
arraySizeparameter is greater than 1 and the graphics device does not support texture arrays.
Texture2D(GraphicsDevice, int, int, bool, SurfaceFormat, int)
Creates an uninitialized Texture2D resource with the specified parameters.
public Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format, int arraySize)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device used to display the texture.
widthintThe width, in pixels, of the texture.
heightintThe height, in pixels, of the texture.
mipmapbooltrue if mimapping is enabled for the texture; otherwise, false.
formatSurfaceFormatThe surface format of the texture.
arraySizeintThe size of the texture array.
Remarks
To initialize the texture with data after creating, you can use one of the SetData method.
To initialize a Texture2D from an existing file use the ContentManager.Load method if loading a pipeline preprocessed Texture2D from an .xnb file or Texture2D.FromFile to load directly from a file.
Exceptions
- ArgumentNullException
The
graphicsDeviceparameter is null.- ArgumentOutOfRangeException
The
widthand/orheightless than or equal to zero.- ArgumentException
The
arraySizeparameter is greater than 1 and the graphics device does not support texture arrays.
Properties
Bounds
Gets the dimensions of the texture
public Rectangle Bounds { get; }
Property Value
Height
Gets the height of the texture in pixels.
public int Height { get; }
Property Value
Width
Gets the width of the texture in pixels.
public int Width { get; }
Property Value
Methods
FromFile(GraphicsDevice, string)
Creates a Texture2D from a file, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures). May work with other formats, but will not work with tga files. This internally calls FromStream(GraphicsDevice, Stream, Action<byte[]>).
public static Texture2D FromFile(GraphicsDevice graphicsDevice, string path)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device to use to create the texture.
pathstringThe path to the image file.
Returns
Remarks
Note that different image decoders may generate slight differences between platforms, but perceptually the images should be identical. This call does not premultiply the image alpha, but areas of zero alpha will result in black color data.
FromFile(GraphicsDevice, string, Action<byte[]>)
Creates a Texture2D from a file, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures). May work with other formats, but will not work with tga files. This internally calls FromFile(GraphicsDevice, string, Action<byte[]>).
public static Texture2D FromFile(GraphicsDevice graphicsDevice, string path, Action<byte[]> colorProcessor)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device to use to create the texture.
pathstringThe path to the image file.
colorProcessorAction<byte[]>Function that is applied to the data in RGBA format before the texture is sent to video memory. Could be null(no processing then).
Returns
Remarks
Note that different image decoders may generate slight differences between platforms, but perceptually the images should be identical.
FromStream(GraphicsDevice, Stream)
Creates a Texture2D from a stream, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures). May work with other formats, but will not work with tga files.
public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device to use to create the texture.
streamStreamThe stream from which to read the image data.
Returns
Remarks
Note that different image decoders may generate slight differences between platforms, but perceptually the images should be identical. This call does not premultiply the image alpha, but areas of zero alpha will result in black color data.
FromStream(GraphicsDevice, Stream, Action<byte[]>)
Creates a Texture2D from a stream, supported formats bmp, gif, jpg, png, tif and dds (only for simple textures). May work with other formats, but will not work with tga files.
public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream, Action<byte[]> colorProcessor)
Parameters
graphicsDeviceGraphicsDeviceThe graphics device to use to create the texture.
streamStreamThe stream from which to read the image data.
colorProcessorAction<byte[]>Function that is applied to the data in RGBA format before the texture is sent to video memory. Could be null(no processing then).
Returns
Remarks
Note that different image decoders may generate slight differences between platforms, but perceptually the images should be identical.
GetData<T>(int, int, Rectangle?, T[], int, int)
Copies texture data into an array
public void GetData<T>(int level, int arraySlice, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
Parameters
levelintThe mipmap level to copy from.
arraySliceintIndex inside the texture array
rectRectangle?The section of the texture where the data will be copied from. null indicates the data will be copied over the entire texture.
dataT[]The array to receive texture data. If
rectis null, the number of elements in the array must be equal to the size of the texture, which is Width x Height; otherwise, the number of elements in the array should be equal to the size of the rectangle.startIndexintThe index of the element in the array at which to start copying.
elementCountintThe number of elements to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
levelparameter is larger than the number of levels in this texture. -
The
arraySliceparameter is greater than zero and the texture arrays are not supported on the graphics device. -
The
arraySliceparameter is less than zero or is greater than or equal to the internal array buffer of this texture. -
The
rectis outside the bounds of the texture. -
The
Ttype size is invalid for the format of this texture. -
The
startIndexparameter is less than zero or is greater than or equal to the length of the data array. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
GetData<T>(int, Rectangle?, T[], int, int)
Copies texture data into an array
public void GetData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
Parameters
levelintThe mipmap level to copy from.
rectRectangle?The section of the texture where the data will be copied from. null indicates the data will be copied over the entire texture.
dataT[]The array to receive texture data. If
rectis null, the number of elements in the array must be equal to the size of the texture, which is Width x Height; otherwise, the number of elements in the array should be equal to the size of the rectangle.startIndexintThe index of the element in the array at which to start copying.
elementCountintThe number of elements to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
levelparameter is larger than the number of levels in this texture. -
The
rectis outside the bounds of the texture. -
The
Ttype size is invalid for the format of this texture. -
The
startIndexparameter is less than zero or is greater than or equal to the length of the data array. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
GetData<T>(T[])
Copies texture data into an array
public void GetData<T>(T[] data) where T : struct
Parameters
dataT[]The array to receive texture data.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
Ttype size is invalid for the format of this texture. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
GetData<T>(T[], int, int)
Copies texture data into an array
public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
Parameters
dataT[]The array to receive texture data.
startIndexintThe index of the element in the array at which to start copying.
elementCountintThe number of elements to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
Ttype size is invalid for the format of this texture. -
The
startIndexparameter is less than zero or is greater than or equal to the length of the data array. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
Reload(Stream)
Reloads the texture
public void Reload(Stream textureStream)
Parameters
textureStreamStream
Remarks
This method allows games that use Texture2D.FromStream to reload their textures after the GL context is lost.
SaveAsJpeg(Stream, int, int)
Converts the texture to a JPG image
public void SaveAsJpeg(Stream stream, int width, int height)
Parameters
streamStreamDestination for the image
widthintThe width, in pixels, of the image.
heightintThe height, in pixels, of the image.
SaveAsPng(Stream, int, int)
Converts the texture to a PNG image
public void SaveAsPng(Stream stream, int width, int height)
Parameters
streamStreamDestination for the image
widthintThe width, in pixels, of the image.
heightintThe height, in pixels, of the image.
SetData<T>(int, int, Rectangle?, T[], int, int)
Copies an array of data to the texture.
public void SetData<T>(int level, int arraySlice, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
Parameters
levelintThe mipmap level where the data will be placed.
arraySliceintIndex inside the texture array
rectRectangle?The section of the texture where the data will be placed. null indicates the data will be copied over the entire texture.
dataT[]The array of data to copy. If
rectis null, the number of elements in the array must be equal to the size of the texture, which is Width x Height; otherwise, the number of elements in the array should be equal to the size of the rectangle.startIndexintThe index of the element in the array at which to start copying.
elementCountintThe number of elements to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
levelparameter is larger than the number of levels in this texture. -
The
arraySliceparameter is greater than zero and the texture arrays are not supported on the graphics device. -
The
arraySliceparameter is less than zero or is greater than or equal to the internal array buffer of this texture. -
The
rectis outside the bounds of the texture. -
The
Ttype size is invalid for the format of this texture. -
The
startIndexparameter is less than zero or is greater than or equal to the length of the data array. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
SetData<T>(int, Rectangle?, T[], int, int)
Copies an array of data to the texture.
public void SetData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
Parameters
levelintThe mipmap level where the data will be placed.
rectRectangle?The section of the texture where the data will be placed. null indicates the data will be copied over the entire texture.
dataT[]The array of data to copy. If
rectis null, the number of elements in the array must be equal to the size of the texture, which is Width x Height; otherwise, the number of elements in the array should be equal to the size of the rectangle.startIndexintThe index of the element in the array at which to start copying.
elementCountintThe number of elements to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
levelparameter is larger than the number of levels in this texture. -
The
rectis outside the bounds of the texture. -
The
Ttype size is invalid for the format of this texture. -
The
startIndexparameter is less than zero or is greater than or equal to the length of the data array. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
SetData<T>(T[])
Copies an array of data to the texture.
public void SetData<T>(T[] data) where T : struct
Parameters
dataT[]The array of data to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
Ttype size is invalid for the format of this texture. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.
SetData<T>(T[], int, int)
Copies an array of data to the texture.
public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
Parameters
dataT[]The array of data to copy.
startIndexintThe index of the element in the array at which to start copying.
elementCountintThe number of elements to copy.
Type Parameters
TThe type of the elements in the array.
Exceptions
- ArgumentException
One of the following conditions is true:
-
The
Ttype size is invalid for the format of this texture. -
The
startIndexparameter is less than zero or is greater than or equal to the length of the data array. -
The
dataarray parameter is too small. length of the data array.
-
The
- ArgumentNullException
The
dataparameter is null.