Recursive AI

Draw a CPU pixel buffer in a native window in C#

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

A CPU pixel buffer is a byte array of colors. Pass it to PresentPixels on Windows or on Linux with Wayland (not X11).

Terminal
dotnet add package RecursiveAI.OnePlatform

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

The buffer uses four bytes per pixel in blue, green, red, unused order, with the top row first.

C#
using System;
using OnePlatform;
using static OnePlatform.PlatformEventKind;
 
using var app = PlatformApp.Create();
const int w = 320, h = 200;
var window = app.CreateWindow(new()
{
    Width = w, Height = h, Resizable = false
});
if (!window.CanPresentPixels)
    throw new NotSupportedException();
byte[] pixels = new byte[w * h * 4];
for (int i = 0; i < pixels.Length; i += 4)
    pixels[i + (i < w * h * 2 ? 0 : 2)] = 255;
window.PresentPixels(pixels, w, h);
Console.WriteLine(
    $"Presented {w}x{h} pixels");
 
while (true)
{
    app.PumpEvents(1000);
    while (app.TryGetEvent(out var evt))
        if (evt.Kind is WindowCloseRequested
            or QuitRequested)
            return;
}

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The snippet draws blue above red in a 320 × 200 window. Use the window’s Close button to exit. It prints:

Text
Presented 320x200 pixels

The full program

Run in a Wayland desktop session or on Windows. The zip contains Example.csproj and Program.cs. No image file is needed: Program.cs fills the pixel buffer in memory.

C#
using System;
using OnePlatform;
using static OnePlatform.PlatformEventKind;
 
using var app = PlatformApp.Create();
const int w = 320, h = 200;
var window = app.CreateWindow(new()
{
    Title = "CPU pixels",
    Width = w, Height = h, Resizable = false
});
if (!window.CanPresentPixels)
    throw new NotSupportedException(
        "CPU pixels aren't supported.");
byte[] pixels = new byte[w * h * 4];
for (int i = 0; i < pixels.Length; i += 4)
    pixels[i + (i < w * h * 2 ? 0 : 2)] = 255;
window.PresentPixels(pixels, w, h);
Console.WriteLine(
    $"Presented {w}x{h} pixels");
 
while (true)
{
    app.PumpEvents(1000);
    while (app.TryGetEvent(out var evt))
        if (evt.Kind is WindowCloseRequested
            or QuitRequested)
            return;
}
Text
Presented 320x200 pixels

If CanPresentPixels is false, the window doesn’t have this presenter; calling PresentPixels on it throws NotSupportedException.

Variations

Replace the loop body that fills pixels with this one to draw a green-and-black checkerboard with 20-pixel squares:

C#
{
    int x = (i / 4) % w;
    int y = (i / 4) / w;
    if ((x / 20 + y / 20) % 2 == 0)
        pixels[i + 1] = 255; // Green channel.
}

The short answer

Allocate four bytes per pixel, fill them in blue, green, red, unused order, then call Window.PresentPixels(pixels, width, height). Start with the top row and pack rows together. The last byte is ignored, so setting it to zero won’t make a pixel transparent. Check CanPresentPixels first: the presenter works on Win32 and on Wayland windows with the shared-memory interface wl_shm. OnePlatform 1.1.0 has no built-in way to display a CPU pixel buffer on X11, macOS, Android, iOS, or in the browser.

How do I draw a pixel array directly into a window?

Start with the dimensions of the image you want to show. A 320 × 200 image has rows of 1,280 bytes and uses 256,000 bytes in total. The byte offset for a pixel in the array is four times its pixel index; the first byte at that offset is blue and the third is red. That’s why the loop writes blue at offset 0 for the first half of the buffer and red at offset 2 for the second half. The green channel stays zero. If your bytes are in red, green, blue, alpha order (RGBA), swap red and blue before presenting. If each row ends with padding bytes, remove them first: the API expects rows with no gaps.

Do I need to redraw the pixel buffer every frame?

Not for an unchanged image. On Wayland, the compositor keeps showing the last committed buffer; on Windows, OnePlatform keeps the image for repainting the client area. Keep pumping window events, but call PresentPixels when the picture changes. This is useful for a static preview or a CPU-rendered tool that updates in response to input.

If you allow the window to resize, use WindowResized to decide when to generate a fresh buffer. For the picture to fill the client area on Wayland, use Window.Width and Window.Height, which report the size in pixels, and pass those same dimensions with the new buffer. This example disables user resizing to keep the pixel layout fixed.

All oneplatform recipes