PImage

A loaded bitmap. Load one with Sketch.LoadImage(path), then draw it with Image().

On this page · 7 entries

LoadImage

  • LoadImage(string path) → PImage

Decodes an image file from disk.

RequestImage

  • RequestImage(string path) → PImage

Starts loading an image on a background thread and returns immediately. The returned image's IsLoaded is false (and Width/Height read as 0) until the background decode finishes, at which point it silently swaps in the real bitmap — check IsLoaded in Draw() before using it, the same way a Processing sketch checks img.width != 0.

ImageMode

  • ImageMode(ShapeAlignMode mode)

Sets how Image()'s x/y/w/h are interpreted — Corner by default (x,y is top-left, w,h is size), Corners (w,h is the opposite corner instead of a size), or Center.

LoadImage

  • LoadImage(byte[] encodedImage) → PImage

Decodes an image that's already in memory — PNG, JPEG, BMP, GIF, WebP: a webcam frame, a download, a resource. Throws if the bytes aren't an image. When you replace one every frame, Dispose() the old PImage.

Image

  • Image(PImage img, x, y)
  • Image(img, x, y, w, h)

Draws the bitmap, respecting the current Tint() and ImageMode().

img.Mask

  • img.Mask(PImage maskImage)

Applies another image as an alpha mask, in place.

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// PImage: Image() draws it, Tint() colors it. LoadImage("photo.png") loads a file
// next to your sketch; this example makes its image in code so it runs anywhere.
public class Images : Sketch
{
    PImage tile;

    public override void Setup()
    {
        Size(600, 300);
        var pg = CreateGraphics(100, 100);
        pg.BeginDraw();
        pg.Background(255);
        pg.NoStroke();
        pg.Fill(255, 120, 80); pg.Circle(50, 50, 70);
        pg.Fill(40);           pg.Circle(50, 50, 20);
        pg.EndDraw();
        tile = pg.Get();
        pg.Dispose();
    }

    public override void Draw()
    {
        Background(30);
        for (int i = 0; i < 5; i++)
        {
Tint(255, 255 - i * 40, 120 + i * 30);
Image(tile, 20 + i * 115, 40, 100, 100);
        }
        NoTint();
        float size = Map(MouseX, 0, Width, 40, 200);
        ImageMode(ShapeAlignMode.Center);
        Image(tile, MouseX, 220, size, size * 0.6f);
        ImageMode(ShapeAlignMode.Corner);
    }
}