The editor
Everything in one component: crop, rotate, straighten, resize and trim; adjustments with a histogram, a colour mixer and white balance; filters; selections with the magic wand; painting; annotations and redaction; layers and groups; before and after; history; export and project files. Drop an image on it, or open one from its own drop zone - then try the theme, direction and language switches.
Try it
The document behind the editor
This is the whole edit: a list of pixel operations, the layers composited over them - paint, images, fills and adjustments - and a list of annotation objects. It is a value, so it can be stored, sent to a server, replayed on the original file, or applied to a different image entirely. It updates as you work.
Load an image into the editor above to see its document here.
Using it
The simple path stays simple: the editor fills its container, so give it a height, and everything else has a working default. The advanced path is the same component with more parameters.
@* The simple path stays simple. *@
<div style="height: 70vh">
<BmageEditor @ref="editor" />
</div>
<button @onclick='() => editor.DownloadAsync("edited")'>Save</button>
@* And the advanced path is the same component. *@
<BmageEditor Strings="german" Class="bmg-theme-dark"
MaxProxyDimension="3000"
OnDocumentChanged="doc => Persist(doc)">
<Toolbar Context="editor">
<button @onclick="() => editor.SetTool(EditorToolKind.Crop)">Crop</button>
<button disabled="@(!editor.CanUndo)" @onclick="editor.Undo">Undo</button>
</Toolbar>
</BmageEditor>
The editor needs the stylesheet _content/Bit.Bmage.Blazor/bit-bmage-editor.css
linked from App.razor or index.html - see
Getting started.
Parameters
| Parameter | Purpose |
|---|---|
Image | The image to edit. Setting it replaces the session and its history. The editor works on a copy, so your buffer stays yours. |
Strings | Every user-visible label - see Localisation. |
Direction | "ltr" or "rtl". The layout uses logical CSS properties, so it mirrors correctly. |
Class, Style | Applied to the root element. bmg-theme-dark or bmg-theme-light force a theme. |
AriaLabel | The accessible name of the editor region, for a page with more than one. |
AcceptedFileTypes | The file types its own drop zone and file picker offer. Default image/*. |
TextRasterizer | What text and step numbers are drawn with. Null uses the FontTextRasterizer from the services, which draws once a font is registered with it - this site registers Noto Sans and Vazirmatn. |
ShowToolbar, ShowPanel | Hide the built-in chrome without replacing it. |
Toolbar, Panel, Footer | Replace the chrome entirely; each receives a BmageEditorContext. |
EmptyState | Replace the "drop an image here" view. |
MaxProxyDimension | Longest edge used for interactive editing. Falls back to BitBmageOptions.MaxInteractiveDimension, then 2048. 0 always edits at full resolution. |
MaxHistoryDepth | Undo steps kept. Default 100. |
ExportOptions | Settings used by the built-in save action: the Export tab starts from its format, quality and metadata policy, and keeps the rest. |
OnDocumentChanged, OnExported, OnImageLoaded, OnError | Callbacks. OnImageLoaded matters more than it looks: the editor can load an image through its own drop zone while your Image is still null, so a page whose buttons depend on an image should re-render from it. |
Driving it from code
Everything the built-in toolbar can do is a public method, and the same surface is handed to
custom chrome through BmageEditorContext - so a toolbar you write can do
everything the built-in one does.
@code {
private BmageEditor? _editor;
void Crop16x9()
{
_editor!.SetTool(EditorToolKind.Crop);
_editor.SetCropAspect(AspectRatio.Ratio16x9);
}
async Task Save() => await _editor!.DownloadAsync("edited",
new ImageExportOptions { Format = ImageFormat.WebP, Quality = 0.9 });
async Task AddCallout() => await _editor!.AddAnnotationAsync(new ArrowAnnotation
{
Start = new Vector2(100, 400),
End = new Vector2(300, 200),
Style = ShapeStyle.Default with { Stroke = new StrokeStyle { Width = 6 } },
});
// Transforms: each one undoable, with annotations and layers kept on what they mark.
async Task Level() => await _editor!.SetStraightenAsync(-2.5);
async Task Shrink() => await _editor!.ResizeAsync(1600, 1200);
async Task Frame() => await _editor!.SetCanvasSizeAsync(2000, 2000, Anchor.Center, Rgba32.White);
async Task Trim() => await _editor!.TrimAsync();
// Selections: select, then act on the pixels inside.
void SelectSky()
{
_editor!.SetSelection(Mask.Gradient(new(0, 0), new(0, 0.4f)));
_editor.InvertSelection(); // ...or everything else
}
async Task CutOut() => await _editor!.ClearSelectionAsync(); // make transparent
async Task Lift() => await _editor!.CopySelectionToLayerAsync(); // a new image layer
async Task CropToIt() => await _editor!.CropToSelectionAsync();
// Layers, before and after, and projects.
async Task Paint() { await _editor!.AddPaintLayerAsync(); _editor.SetTool(EditorToolKind.Brush); }
async Task Compare() => await _editor!.SetCompareAsync(!_editor.IsComparing);
async Task Keep() => await _editor!.SaveProjectAsync("holiday"); // a .bmage file
async Task Reopen(IBrowserFile file) => await _editor!.OpenProjectAsync(file);
}
For an adjustment driven by your own slider, pass transient: true while dragging
and commit once when the interaction ends, so the history gets one entry rather than hundreds.
Replacing the chrome
Toolbar, Panel and Footer take a render fragment with a
BmageEditorContext: the current tool and settings, the document, the annotations and
layers, the selection, the history and its position, the detected capabilities, the output size,
and every action - tool selection, crop, rotate, flip, straighten, resize, canvas size, trim,
adjustments, white balance, annotations, layers and groups, selections, compare, undo, redo,
reset, zoom, load, paste, export, download, clipboard copy and projects.
<BmageEditor ExportOptions="_options">
<Toolbar Context="editor">
<div class="my-toolbar">
<button class="@(editor.Tool == EditorToolKind.Crop ? "on" : null)"
@onclick="() => editor.SetTool(EditorToolKind.Crop)">
Crop
</button>
<button disabled="@(!editor.CanUndo)" @onclick="editor.Undo">Undo</button>
<button disabled="@(!editor.CanRedo)" @onclick="editor.Redo">Redo</button>
<span>@editor.OutputSize.Width × @editor.OutputSize.Height</span>
<button @onclick='() => editor.DownloadAsync("image")'>Save</button>
</div>
</Toolbar>
</BmageEditor>Theming
Every colour and metric is a CSS custom property on .bmg-editor. Override the
ones you care about - this site does exactly that to make the editor follow the theme switch
in the header rather than the operating system (the site theme above), while
light and dark show the component's own palettes.
.my-editor {
--bmg-accent: #7c3aed;
--bmg-accent-text: #ffffff;
--bmg-bg: #0f0f12;
--bmg-bg-subtle: #17171c;
--bmg-canvas-bg: #202027;
--bmg-border: #2a2a33;
--bmg-text: #eeeef2;
--bmg-radius: 4px;
--bmg-panel-width: 320px;
}
/* <BmageEditor Class="my-editor" /> */
Dark mode follows prefers-color-scheme unless forced with
bmg-theme-light or bmg-theme-dark. High contrast mode and
prefers-reduced-motion are honoured.
Localisation
BmageEditorStrings is a record with a property for every label, so a translation
is an object rather than a resource file - and you can override only the words you care
about. Number and size formatting are functions on the same record, and so are the names of
annotation and layer kinds, blend modes and anchors. For a right-to-left language set
Direction="rtl": the panel, the crop handles and the toolbar all mirror. The
Persian button above switches both at once; both translations set every label.
Text and step numbers are drawn into the image from font files, the same way on a server as here: this site registers Noto Sans and Vazirmatn, and a letter one lacks comes from the other. A Persian or Arabic label is shaped with the font's own OpenType rules - joined letters, lam-alef, harakat in place - and laid out right to left by the Unicode Bidirectional Algorithm, Latin words and numbers inside it included. Try the Text tool with the Persian button on.
var german = BmageEditorStrings.Default with
{
Crop = "Zuschneiden",
Undo = "Rückgängig",
Redo = "Wiederholen",
Download = "Herunterladen",
DropImageHere = "Bild hier ablegen oder klicken zum Auswählen",
};
// Formatting is replaceable too, because it is a function on the same record.
var strings = BmageEditorStrings.Default with
{
FormatFileSize = bytes => $"{bytes / 1024.0:0.#} Ko",
FormatDimensions = (w, h) => $"{w}×{h} px",
};
// <BmageEditor Strings="german" Direction="ltr" />The panel and the toolbar
The built-in panel is seven tabs, each a plain form over the same public methods - copy the calls you need into a panel of your own.
| Tab | What it holds |
|---|---|
| Adjust | A live histogram with how much of the image is clipped to black or white; auto levels, automatic white balance and a white balance picker - click something that should be grey; brightness, contrast, saturation, exposure, temperature, vibrance, shadows, highlights, whites, blacks, tint, hue, sharpen, blur and vignette; and a colour mixer that shifts the hue, saturation and lightness of one of eight colour bands. |
| Filters | Looks - grayscale, sepia, invert, posterize, threshold, black and white, duotone - and a motion blur with a distance and an angle. |
| Transform | Straighten up to 45° with the corners cropped away; resize to exact numbers, proportions kept or not; canvas size with an anchor and a colour or transparency; and trim to the content. |
| Tools | Stroke and fill colours, dashes, width, opacity and font size for the drawing tools; brush size and hardness; the tolerance of the fill, magic eraser and magic wand; the redaction style - and, for the selected annotation, its text, size, rotation, opacity, order and lock. |
| Layers | Add a paint layer or an image as a layer; every layer with show, hide, lock, reorder, duplicate and delete; the selected one's opacity, blend mode and clipping to the layer below; group it with the layer below, move it out of its group, ungroup, and blend a group as one. Annotations are listed under the layers. |
| History | Every step by name; click one to go back to it, or forward again. |
| Export | The output size (and the original's, while editing through a proxy); format, quality and metadata policy; download and copy to the clipboard; save the edit as a project, and open one. |
The toolbar holds the tools below, rotate and flip, undo, redo and reset, Compare with the original - which shows the original until it is turned off - and zoom. While pixels are selected it adds the selection's actions: make them transparent, fill them, copy them to a layer, crop to them, invert, and deselect.
Tools and keyboard
| Tool | Behaviour |
|---|---|
| Select | Click to select an annotation, drag to move it, arrow keys to nudge, Delete to remove. |
| Pan | Drag to move the view. |
| Crop | Drag a rectangle, adjust it with eight handles, apply from the toolbar. |
| Draw | Freehand, smoothed so fast strokes do not look polygonal. |
| Line, Arrow | Drag from start to end; hold Shift to snap to 15° increments. |
| Rectangle, Ellipse | Drag to size; hold Shift for a square or a circle. |
| Highlight | A translucent block, like a marker pen. |
| Redact | Solid, pixelate or blur; replaces the pixels underneath. |
| Text | Places a label you then edit from the Tools tab. |
| Step | Auto-numbered markers for tutorials and bug reports. |
| Brush | Paints onto the selected paint layer, or the topmost one, or a new one when there is none - with a round brush, soft or hard, narrowing with pen pressure. |
| Eraser | Removes paint from the paint layer and never touches the image below. |
| Pick a colour | The eyedropper: picks the colour under the pointer as the stroke and fill colour - or, started from the Adjust tab's picker, as the grey a white balance should make neutral. |
| Fill | Fills the region of similar colour under the pointer, as a new layer you can still change. |
| Magic eraser | Makes the region of similar colour under the pointer transparent. |
| Rectangle select, Ellipse select, Lasso | Select pixels by shape or by drawing around them, outlined with marching ants. Hold Shift to add to the selection, Alt to take away from it. |
| Magic wand | Selects the region of similar colour under the pointer, within the tolerance; Shift and Alt add and subtract here too. |
| Keys | Action |
|---|---|
| Ctrl / ⌘ + Z | Undo |
| Ctrl + Shift + Z, Ctrl + Y | Redo |
| Delete, Backspace | Delete the selected annotation, or make the selected pixels transparent |
| Arrow keys | Nudge the selected annotation by 1px, or 10px with Shift |
| Ctrl + A | Select all the pixels |
| Ctrl + D | Deselect |
| Ctrl + Shift + I | Invert the selection |
| Esc | Clear the annotation and pixel selections, or cancel a pending crop |
| \ | Compare with the original, and back |
| [, ] | Make the brush or eraser smaller or larger |
| +, - | Zoom |
| Ctrl + 0 | Fit to window |
The canvas is a focusable application region with a described keyboard hint. The
toolbar is a toolbar of labelled buttons carrying aria-pressed, the
panel is a tablist with arrow-key navigation, crop handles are focusable buttons
with 24px hit targets, and status messages use live regions. On touch screens the canvas
claims the drag (so drawing does not scroll the page) and the panel moves below the canvas.
Large images
By default the editor edits through a proxy no larger than 2048px on its longest edge and
replays the edit at full resolution on export - for a 24 megapixel photo, a 25× cut in
the pixels each preview touches. Because every operation rescales itself, the preview and the
export are one edit at two resolutions rather than two code paths that could disagree.
ImageEditSession.UsesProxy and ProxyScale say when it is happening;
set MaxProxyDimension="0" to always edit at full resolution, or
MaxInteractiveDimension on BitBmageOptions to change the default for
every editor in the app.
History stores descriptions, not pixels: a hundred undo steps on a 6000 × 4000 image stay under a megabyte.