Skip to content

GraphicsAPI reference

Class PixelImage

Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Graphics2D.dll
Guide: Graphics

A CPU-side RGBA image. Games author procedural art with it and hand it to CreateTexture; it also backs the built-in font.

C#
public sealed class PixelImage

Inheritance

object ← PixelImage

Remarks

Drawing methods change this image and return it, so calls chain; methods that build a new image (Clone, FlipX, Crop…) leave it unchanged. Coordinates outside the image are ignored when writing and read as transparent. Drawing methods alpha-blend unless they say otherwise.

Constructors

PixelImage(int, int)

Creates a fully transparent image.

C#
public PixelImage(int width, int height)

Parameters

width int

height int

Exceptions

ArgumentOutOfRangeException

width or height is not positive.

Properties

Height

Height in pixels.

C#
public int Height { get; }

Property Value

int

Pixels

The pixels, row by row from the top-left.

C#
public ReadOnlySpan<Color> Pixels { get; }

Property Value

ReadOnlySpan<Color>

Width

Width in pixels.

C#
public int Width { get; }

Property Value

int

this[int, int]

The pixel at (x, y). Reading outside the image returns Transparent; writing outside does nothing. Writing replaces the pixel without blending.

C#
public Color this[int x, int y] { get; set; }

Property Value

Color

Methods

Blend(int, int, Color)

Alpha-blends color over the existing pixel.

C#
public void Blend(int x, int y, Color color)

Parameters

x int

y int

color Color

Blit(PixelImage, int, int, bool)

Copies another image (alpha-blended) with optional horizontal flip.

C#
public PixelImage Blit(PixelImage source, int dx, int dy, bool flipX = false)

Parameters

source PixelImage

dx int

dy int

flipX bool

Returns

PixelImage

Blit(PixelImage, int, int, int, int, int, int)

Blends the w × h region of source at (sx, sy) onto this image at (dx, dy).

C#
public PixelImage Blit(PixelImage source, int sx, int sy, int w, int h, int dx, int dy)

Parameters

source PixelImage

sx int

sy int

w int

h int

dx int

dy int

Returns

PixelImage

Clear(Color)

Sets every pixel to color, without blending.

C#
public PixelImage Clear(Color color)

Parameters

color Color

Returns

PixelImage

ClearRect(int, int, int, int)

Makes a rectangle fully transparent (unlike FillRect, which blends).

C#
public PixelImage ClearRect(int x, int y, int w, int h)

Parameters

x int

y int

w int

h int

Returns

PixelImage

Clone()

Returns an independent copy.

C#
public PixelImage Clone()

Returns

PixelImage

Crop(int, int, int, int)

Copies a rectangle into a new image; areas outside the source become transparent.

C#
public PixelImage Crop(int x, int y, int width, int height)

Parameters

x int

y int

width int

height int

Returns

PixelImage

Decode(ReadOnlySpan<byte>)

Decodes an image file held in memory: PNG (any variant), JPEG (baseline and progressive), BMP, TGA, GIF (its first frame) or PSD (the composited image), recognised by its content rather than a name.

C#
public static PixelImage Decode(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

Returns

PixelImage

Exceptions

InvalidDataException

The data is not an image in one of those formats.

DrawRect(int, int, int, int, Color)

Draws the one-pixel outline of a rectangle.

C#
public PixelImage DrawRect(int x, int y, int w, int h, Color color)

Parameters

x int

y int

w int

h int

color Color

Returns

PixelImage

FillCircle(float, float, float, Color)

Filled circle centred on a pixel-space coordinate (half-pixel centres allowed).

C#
public PixelImage FillCircle(float cx, float cy, float radius, Color color)

Parameters

cx float

cy float

radius float

color Color

Returns

PixelImage

FillEllipse(float, float, float, float, Color)

Fills an ellipse centred on (cx, cy) with radii rx and ry.

C#
public PixelImage FillEllipse(float cx, float cy, float rx, float ry, Color color)

Parameters

cx float

cy float

rx float

ry float

color Color

Returns

PixelImage

FillRect(int, int, int, int, Color)

Blends color over a w × h rectangle whose top-left is (x, y).

C#
public PixelImage FillRect(int x, int y, int w, int h, Color color)

Parameters

x int

y int

w int

h int

color Color

Returns

PixelImage

FitInside(int, int)

The largest size of this image that fits inside maxWidth by maxHeight, keeping its shape (at least 1 by 1).

C#
public (int Width, int Height) FitInside(int maxWidth, int maxHeight)

Parameters

maxWidth int

maxHeight int

Returns

(int Width, int Height)

FlipX()

Returns a copy mirrored horizontally.

C#
public PixelImage FlipX()

Returns

PixelImage

FlipY()

Returns a copy mirrored vertically.

C#
public PixelImage FlipY()

Returns

PixelImage

FromMap(IReadOnlyDictionary<char, Color>, params string[])

Creates an image from a character map; the image size is the map size.

C#
public static PixelImage FromMap(IReadOnlyDictionary<char, Color> palette, params string[] rows)

Parameters

palette IReadOnlyDictionary<char, Color>

rows string[]

Returns

PixelImage

FromPng(ReadOnlySpan<byte>)

Decodes a PNG: any colour type and bit depth, interlaced or not.

C#
public static PixelImage FromPng(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

Returns

PixelImage

Exceptions

InvalidDataException

The data is not a PNG the engine can read.

InBounds(int, int)

Whether (x, y) is inside the image.

C#
public bool InBounds(int x, int y)

Parameters

x int

y int

Returns

bool

Line(int, int, int, int, Color)

Draws a one-pixel line between two pixels, both included.

C#
public PixelImage Line(int x0, int y0, int x1, int y1, Color color)

Parameters

x0 int

y0 int

x1 int

y1 int

color Color

Returns

PixelImage

Load(string)

Loads an image file in any format Decode reads; the message names the path when decoding fails.

C#
public static PixelImage Load(string path)

Parameters

path string

Returns

PixelImage

Exceptions

FileNotFoundException

The file does not exist.

InvalidDataException

The file is not an image the engine can read.

Map(Func<int, int, Color, Color>)

Replaces each pixel with transform(x, y, pixel).

C#
public PixelImage Map(Func<int, int, Color, Color> transform)

Parameters

transform Func<int, int, Color, Color>

Returns

PixelImage

OpaqueBounds()

Smallest rectangle containing every pixel with alpha above zero; null when fully transparent.

C#
public (int X, int Y, int Width, int Height)? OpaqueBounds()

Returns

(int X, int Y, int Width, int Height)?

Outline(Color, bool)

Adds a 1px outline of color around every opaque region (4-neighbourhood).

C#
public PixelImage Outline(Color color, bool diagonals = false)

Parameters

color Color

diagonals bool

Returns

PixelImage

Paint(int, int, IReadOnlyDictionary<char, Color>, params string[])

Paints a character map. Each row is a string; each char is looked up in palette. Characters missing from the palette (conventionally ‘.’ or ’ ’) are left untouched.

C#
public PixelImage Paint(int dx, int dy, IReadOnlyDictionary<char, Color> palette, params string[] rows)

Parameters

dx int

dy int

palette IReadOnlyDictionary<char, Color>

rows string[]

Returns

PixelImage

Recolor(Color, Color)

Replaces every exact from pixel with to.

C#
public PixelImage Recolor(Color from, Color to)

Parameters

from Color

to Color

Returns

PixelImage

Resized(int, int)

A copy at width by height pixels. Whole-number enlargements repeat pixels, so pixel art stays sharp; other sizes average the source area under each pixel (weighted by opacity, so transparent pixels do not darken edges), so a 1024-pixel icon reduced for a launcher or a start screen keeps its detail instead of dropping rows.

C#
public PixelImage Resized(int width, int height)

Parameters

width int

height int

Returns

PixelImage

Exceptions

ArgumentOutOfRangeException

A size is not positive.

Rotate90()

Returns a copy rotated 90 degrees clockwise (width and height swap).

C#
public PixelImage Rotate90()

Returns

PixelImage

Rotated(float, float, float)

Returns a same-size copy rotated by radians (clockwise on screen) around a pivot, sampling nearest neighbours so the palette stays intact. Content leaving the canvas is clipped. Useful for deriving extra poses (e.g. aiming arms) from authored frames.

C#
public PixelImage Rotated(float radians, float pivotX, float pivotY)

Parameters

radians float

pivotX float

pivotY float

Returns

PixelImage

Scale2x()

Doubles the image with the Scale2x (EPX) rule: flat areas stay flat while one-pixel stair steps become slopes, so small sprites can be shown at twice the size without looking blocky. Pixels beyond the edge count as copies of the edge pixel.

C#
public PixelImage Scale2x()

Returns

PixelImage

Silhouette(Color)

Returns a copy where every visible pixel becomes color (hit flashes, shadows).

C#
public PixelImage Silhouette(Color color)

Parameters

color Color

Returns

PixelImage

ToRgba()

Raw RGBA8 bytes, row-major, for backends.

C#
public byte[] ToRgba()

Returns

byte[]