Table of Contents

Class Texture2D

Namespace
Microsoft.Xna.Framework.Graphics
Assembly
MonoGame.Framework.dll
public class Texture2D : Texture, IDisposable
Inheritance
Texture2D
Implements
Derived
Inherited Members

Constructors

Texture2D(GraphicsDevice, int, int)

Creates a new texture of the given size

public Texture2D(GraphicsDevice graphicsDevice, int width, int height)

Parameters

graphicsDevice GraphicsDevice
width int
height int

Texture2D(GraphicsDevice, int, int, bool, SurfaceFormat)

Creates a new texture of a given size with a surface format and optional mipmaps

public Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format)

Parameters

graphicsDevice GraphicsDevice
width int
height int
mipmap bool
format SurfaceFormat

Texture2D(GraphicsDevice, int, int, bool, SurfaceFormat, SurfaceType, bool, int)

protected Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format, Texture2D.SurfaceType type, bool shared, int arraySize)

Parameters

graphicsDevice GraphicsDevice
width int
height int
mipmap bool
format SurfaceFormat
type Texture2D.SurfaceType
shared bool
arraySize int

Texture2D(GraphicsDevice, int, int, bool, SurfaceFormat, int)

Creates a new texture array of a given size with a surface format and optional mipmaps. Throws ArgumentException if the current GraphicsDevice can't work with texture arrays

public Texture2D(GraphicsDevice graphicsDevice, int width, int height, bool mipmap, SurfaceFormat format, int arraySize)

Parameters

graphicsDevice GraphicsDevice
width int
height int
mipmap bool
format SurfaceFormat
arraySize int

Properties

Bounds

Gets the dimensions of the texture

public Rectangle Bounds { get; }

Property Value

Rectangle

Height

Gets the height of the texture in pixels.

public int Height { get; }

Property Value

int

Width

Gets the width of the texture in pixels.

public int Width { get; }

Property Value

int

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

graphicsDevice GraphicsDevice

The graphics device to use to create the texture.

path string

The path to the image file.

Returns

Texture2D

The Texture2D created from the given file.

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 FromStream(GraphicsDevice, Stream, Action<byte[]>).

public static Texture2D FromFile(GraphicsDevice graphicsDevice, string path, Action<byte[]> colorProcessor)

Parameters

graphicsDevice GraphicsDevice

The graphics device to use to create the texture.

path string

The path to the image file.

colorProcessor Action<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

Texture2D

The Texture2D created from the given file.

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

graphicsDevice GraphicsDevice

The graphics device to use to create the texture.

stream Stream

The stream from which to read the image data.

Returns

Texture2D

The Texture2D created from the image stream.

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

graphicsDevice GraphicsDevice

The graphics device to use to create the texture.

stream Stream

The stream from which to read the image data.

colorProcessor Action<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

Texture2D

The Texture2D created from the image stream.

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)

Retrieves the contents of the texture Throws ArgumentException if data is null, data.length is too short or if arraySlice is greater than 0 and the GraphicsDevice doesn't support texture arrays

public void GetData<T>(int level, int arraySlice, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct

Parameters

level int

Layer of the texture

arraySlice int

Index inside the texture array

rect Rectangle?

Area of the texture to retrieve

data T[]

Destination array for the data

startIndex int

Starting index of data where to write the pixel data

elementCount int

Number of pixels to read

Type Parameters

T

GetData<T>(int, Rectangle?, T[], int, int)

Retrieves the contents of the texture Throws ArgumentException if data is null, data.length is too short or if arraySlice is greater than 0 and the GraphicsDevice doesn't support texture arrays

public void GetData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct

Parameters

level int

Layer of the texture

rect Rectangle?

Area of the texture

data T[]

Destination array for the texture data

startIndex int

First position in data where to write the pixel data

elementCount int

Number of pixels to read

Type Parameters

T

GetData<T>(T[])

Retrieves the contents of the texture Throws ArgumentException if data is null, data.length is too short or if arraySlice is greater than 0 and the GraphicsDevice doesn't support texture arrays

public void GetData<T>(T[] data) where T : struct

Parameters

data T[]

Destination array for the texture data

Type Parameters

T

GetData<T>(T[], int, int)

Retrieves the contents of the texture Throws ArgumentException if data is null, data.length is too short or if arraySlice is greater than 0 and the GraphicsDevice doesn't support texture arrays

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

Parameters

data T[]

Destination array for the texture data

startIndex int

First position in data where to write the pixel data

elementCount int

Number of pixels to read

Type Parameters

T

Reload(Stream)

public void Reload(Stream textureStream)

Parameters

textureStream Stream

SaveAsJpeg(Stream, int, int)

Converts the texture to a JPG image

public void SaveAsJpeg(Stream stream, int width, int height)

Parameters

stream Stream

Destination for the image

width int
height int

SaveAsPng(Stream, int, int)

Converts the texture to a PNG image

public void SaveAsPng(Stream stream, int width, int height)

Parameters

stream Stream

Destination for the image

width int
height int

SetData<T>(int, int, Rectangle?, T[], int, int)

Changes the pixels of the texture Throws ArgumentNullException if data is null Throws ArgumentException if arraySlice is greater than 0, and the GraphicsDevice does not support texture arrays

public void SetData<T>(int level, int arraySlice, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct

Parameters

level int

Layer of the texture to modify

arraySlice int

Index inside the texture array

rect Rectangle?

Area to modify

data T[]

New data for the texture

startIndex int

Start position of data

elementCount int

Type Parameters

T

SetData<T>(int, Rectangle?, T[], int, int)

Changes the pixels of the texture

public void SetData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct

Parameters

level int

Layer of the texture to modify

rect Rectangle?

Area to modify

data T[]

New data for the texture

startIndex int

Start position of data

elementCount int

Type Parameters

T

SetData<T>(T[])

Changes the texture's pixels

public void SetData<T>(T[] data) where T : struct

Parameters

data T[]

Type Parameters

T

New data for the texture

SetData<T>(T[], int, int)

Changes the texture's pixels

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

Parameters

data T[]

New data for the texture

startIndex int

Start position of data

elementCount int

Type Parameters

T