Skip to content
CShift0.32

Image

Struct · namespace System.Image · Since 0.28

struct Image

An image: Width times Height pixels, stored row by row from the top left as 32-bit ARGB values (0xAARRGGBB, not premultiplied) in Pixels.

var image = Image.Create(16, 16); // transparent black
image.SetPixel(3, 4, Color.White());
uint32 argb = image.Pixels[4 * image.Width + 3]; // the same pixel, directly
try image.Save("glyph.png");

Like a List, an image is a small struct that points at its pixels: copies share them; Image.Clone copies them. Images are decoded into this one format, whatever the file had (a palette, grayscale, 16 bits per channel: those keep the 8 highest bits); saving chooses the format of the file (ImageFormat).

Name Description
Width The width in pixels.
Height The height in pixels.
Pixels The pixels, row by row from the top left: Pixels[y * Width + x] is 0xAARRGGBB.
int Width

The width in pixels.

int Height

The height in pixels.

uint32[] Pixels

The pixels, row by row from the top left: Pixels[y * Width + x] is 0xAARRGGBB.

Name Description
Create A new image of width times height transparent black pixels (0).
Load Reads an image file: PNG, BMP or PPM/PGM/PBM, recognized by its content (not the extension).
Decode Decodes an image from the bytes of a PNG, BMP or PPM/PGM/PBM file.
DetectFormat The format of the bytes of an image file, recognized by its first bytes; null for none of PNG, BMP and PPM/PGM/PBM.
FormatOfPath The format that belongs to the extension of path (.png, .bmp/.dib, .ppm/.pnm; any case), or null.
static Image Create(int width, int height)

A new image of width times height transparent black pixels (0).

Panics when a size is negative.

static Image Create(int width, int height, Color fill)

A new image of width times height pixels of the color fill.

Panics when a size is negative.

static ImageError<Image> Load(StringSlice path)

Reads an image file: PNG, BMP or PPM/PGM/PBM, recognized by its content (not the extension).

Errors

static ImageError<Image> Decode(ReadOnlySlice<uint8> data)

Decodes an image from the bytes of a PNG, BMP or PPM/PGM/PBM file.

Errors

static Optional<ImageFormat> DetectFormat(ReadOnlySlice<uint8> data)

The format of the bytes of an image file, recognized by its first bytes; null for none of PNG, BMP and PPM/PGM/PBM.

static Optional<ImageFormat> FormatOfPath(StringSlice path)

The format that belongs to the extension of path (.png, .bmp/.dib, .ppm/.pnm; any case), or null.

Name Description
Save Writes the image to a file in the format of its extension (Image.FormatOfPath).
Encode The bytes of the image as a file in format.
GetPixel The color of the pixel at x, y (0, 0 is the top left).
SetPixel Sets the pixel at x, y to color.
GetArgb The pixel at x, y as 0xAARRGGBB.
SetArgb Sets the pixel at x, y to 0xAARRGGBB.
Clear Sets all pixels to color.
Clone A copy with its own pixels.
Crop A new image of the part width times height from x, y.
Copy Copies the pixels of source to x, y (they replace the pixels there, alpha included: no blending).
ImageError<void> Save(StringSlice path)

Writes the image to a file in the format of its extension (Image.FormatOfPath).

Errors

ImageError<void> Save(StringSlice path, ImageFormat format)

Writes the image to a file in format, whatever the extension.

Errors

uint8[] Encode(ImageFormat format)

The bytes of the image as a file in format.

Color GetPixel(int x, int y)

The color of the pixel at x, y (0, 0 is the top left).

Panics when the position is outside of the image.

void SetPixel(int x, int y, Color color)

Sets the pixel at x, y to color.

Panics when the position is outside of the image.

uint32 GetArgb(int x, int y)

The pixel at x, y as 0xAARRGGBB.

Panics when the position is outside of the image.

void SetArgb(int x, int y, uint32 argb)

Sets the pixel at x, y to 0xAARRGGBB.

Panics when the position is outside of the image.

void Clear(Color color)

Sets all pixels to color.

Image Clone()

A copy with its own pixels.

Image Crop(int x, int y, int width, int height)

A new image of the part width times height from x, y.

Panics when the part is not inside of the image.

void Copy(const ref Image source, int x, int y)

Copies the pixels of source to x, y (they replace the pixels there, alpha included: no blending). Parts outside of this image are left out.

void Copy(const ref Image source, int sourceX, int sourceY, int width, int height, int x, int y)

Copies the part width times height from sourceX, sourceY of source to x, y (the pixels replace the ones there: no blending). Parts outside of either image are left out; source may be this image, and the parts may overlap.