Use SetIcon on Linux with X11 or a Wayland desktop that supports window icons. OnePlatform can’t set per-window icons on Windows or macOS yet.
dotnet add package RecursiveAI.OnePlatformRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePlatform; using var app = PlatformApp.Create(); var window = app.CreateWindow(new() { Title = "Blue icon" }); var rgba = new byte[32 * 32 * 4]; for (int i = 0; i < rgba.Length; i += 4) { rgba[i + 2] = 255; rgba[i + 3] = 255; } var icon = new IconImage(32, 32, rgba); var applied = window.SetIcon([icon]); Console.WriteLine($"Icon set: {applied}");
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 generates a solid blue square in memory and exits after setting the icon. To inspect the window, run the full program from the ZIP; it stays open until you click the Blue icon window’s close button (×). On X11 it prints:
Icon set: TrueFull program
using OnePlatform; using Kind = OnePlatform.PlatformEventKind; using var app = PlatformApp.Create(); var window = app.CreateWindow(new() { Title = "Blue icon" }); var rgba = new byte[32 * 32 * 4]; for (int i = 0; i < rgba.Length; i += 4) { rgba[i + 2] = 255; rgba[i + 3] = 255; } var icon = new IconImage(32, 32, rgba); var applied = window.SetIcon([icon]); Console.WriteLine($"Icon set: {applied}"); while (true) { app.PumpEvents(100); while (app.TryGetEvent(out var e)) if (e.Kind is Kind.WindowCloseRequested or Kind.QuitRequested) return; }
If SetIcon returns false, this window or backend doesn’t support setting the icon through this API.
Variations
If you have artwork at two sizes, pass both pictures. Replace the last two lines of the snippet. This version uses the example’s blue pixels to make a second, 16 × 16 picture:
var smallPixels = rgba.AsSpan(0, 16 * 16 * 4); var small = new IconImage(16, 16, smallPixels); var applied = window.SetIcon([small, icon]); Console.WriteLine($"Icon set: {applied}");
Related recipes
The short answer
Call window.SetIcon([icon]), where icon is an IconImage containing red, green, blue, and alpha (RGBA) bytes that haven’t been premultiplied by alpha. OnePlatform publishes the images as _NET_WM_ICON on X11. On Wayland, it sends them through xdg_toplevel_icon_manager_v1 for a top-level window; they take effect on the window’s next surface commit.
How do you set a window icon from an image in C#?
Supply four bytes per pixel in red, green, blue, alpha order, starting at the top row. Straight alpha means the color channels haven’t already been multiplied by alpha. IconImage copies the input, so you can reuse the source buffer afterward. Pass decoded pixels, not the encoded bytes from a PNG or ICO file. The constructor requires a width and height from 1 to 1024 and exactly width × height × 4 bytes.
To decode a PNG, install RecursiveAI.OneImage with dotnet add package RecursiveAI.OneImage. Supply your own icon.png. OneImage’s pixel access API returns 8-bit RGBA pixels; use their bytes to build the IconImage you pass to SetIcon.
using System.Runtime.InteropServices; using OneImage; using OnePlatform; var image = Image.Load("icon.png"); var pixels = image.ToArray<Rgba32>(); var rgba = MemoryMarshal.AsBytes(pixels.AsSpan()); var icon = new IconImage(image.Width, image.Height, rgba);
You can pass one square image and let OnePlatform make smaller copies. The 32-pixel square in the example also produces 16-, 22-, and 24-pixel copies. If you’ve drawn alternate artwork for small sizes, pass those images explicitly as in the variation. When given multiple pictures, OnePlatform uses that set instead of generating smaller copies.
Is a window icon the same as the application icon?
No. SetIcon publishes pictures for a particular window. It doesn’t install or change your application’s launcher icon. An installed Linux launcher can supply an icon via its .desktop entry. On Wayland the compositor can match that entry using the AppId you set when creating the window. That’s the route to use when your compositor doesn’t offer the icon protocol or when you need an icon before the app is running.
On Wayland, popups and overlays don’t take a window icon through this API. On Windows and macOS the call returns false in this release. See the window icon backend table before depending on a per-window icon for your app’s identity.