Image
Struct · namespace System.Image · Since 0.28
struct ImageAn 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 blackimage.SetPixel(3, 4, Color.White());uint32 argb = image.Pixels[4 * image.Width + 3]; // the same pixel, directlytry 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).
Fields
Section titled “Fields”| 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 WidthThe width in pixels.
Height
Section titled “Height”int HeightThe height in pixels.
Pixels
Section titled “Pixels”uint32[] PixelsThe pixels, row by row from the top left: Pixels[y * Width + x] is 0xAARRGGBB.
Static methods
Section titled “Static methods”| 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. |
Create
Section titled “Create”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
ImageError.CannotOpen: the file does not exist or cannot be read.ImageError.InvalidData: the file is damaged.ImageError.Unsupported: the file has another format.
Decode
Section titled “Decode”static ImageError<Image> Decode(ReadOnlySlice<uint8> data)Decodes an image from the bytes of a PNG, BMP or PPM/PGM/PBM file.
Errors
ImageError.InvalidData: the data is damaged.ImageError.Unsupported: the data has another format.
DetectFormat
Section titled “DetectFormat”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.
FormatOfPath
Section titled “FormatOfPath”static Optional<ImageFormat> FormatOfPath(StringSlice path)The format that belongs to the extension of path (.png, .bmp/.dib, .ppm/.pnm; any case), or
null.
Methods
Section titled “Methods”| 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.Unsupported: the extension is none of the formats.ImageError.CannotWrite: the file cannot be created or written.
ImageError<void> Save(StringSlice path, ImageFormat format)Writes the image to a file in format, whatever the extension.
Errors
ImageError.CannotWrite: the file cannot be created or written.
Encode
Section titled “Encode”uint8[] Encode(ImageFormat format)The bytes of the image as a file in format.
GetPixel
Section titled “GetPixel”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.
SetPixel
Section titled “SetPixel”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.
GetArgb
Section titled “GetArgb”uint32 GetArgb(int x, int y)The pixel at x, y as 0xAARRGGBB.
Panics when the position is outside of the image.
SetArgb
Section titled “SetArgb”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.