Tavenem.Blazor.ImageEditor is a Razor class library (RCL) containing a Razor component. It allows displaying an image with basic image edit controls, and has mechanisms for saving and restoring the edited file.
It wraps the Fabric.js HTML5 canvas library.
Tavenem.Blazor.ImageEditor is available as a NuGet package.
-
Call the
AddImageEditor()
extension method on yourIServiceCollection
(probably inProgram.Main
for a Blazor WebAssembly project, orStartup.ConfigureServices
for a Blazor server project). -
Add the following css reference to the head section of your index.html (for Blazor WebAssembly) or _Host.cshtml (for Blazor server):
<link rel="stylesheet" href="_content/Tavenem.Blazor.ImageEditor/style.css" />
Note that you must enable static files to serve content from a RCL.
Why is the custom stylesheet not included via Blazor CSS isolation? Because it must apply outside the scope of the component. It refers to a dynamically-generated DOM element managed by the Fabric.js library which is appended to the body.
-
Add the following script reference to the bottom of the body section of your index.html (for Blazor WebAssembly) or _Host.cshtml (for Blazor server):
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/460/fabric.min.js"></script>
-
Include an
ImageEditor
component on a page. -
To set an initial image, call the
LoadImageAsync(string?)
method with the URL of an image you wish to display.Alternatively, you can omit this step and the editor will start as a blank canvas onto which a user may draw.
-
The user may toggle the control from preview mode (displays the original image) to edit mode with a button.
Alternatively, you may set the
ShowEditButton
parameter to false to disable the edit mode button. In that case you can toggle edit mode programmatically with theBeginEditAsync
andCancelEditAsync
methods. You can also supply custom UI to replace the default button (see below for more information). -
When in edit mode, the component displays a selection of common image editing controls which allow drawing onto the image.
Alternatively, you may set the
ShowEditControls
parameter to false to disable the default edit UI. In that case you would need to supply custom UI to enable image editing (see below for more information). -
The default UI includes a "download" button that will invoke the browser's file save option, to save a copy of their edited image. You may also choose to provide values for the
GetObjUrlCallback
and/orSaveJsonCallback
parameters. If you do the user will also see an additional "save" button which will invoke both of these callbacks.GetObjUrlCallback
will be passed an object URL referencing the current, edited image. You may use this to work with the edited image. For example, to get the image as a byte array for persisting to storage:var bytes = await HttpClient.GetByteArrayAsync(url);
The object URL will remain valid as long as the
ImageEditor
component remains active. When the component is disposed, all object URLs it generated will be revoked.The
SaveJsonCallback
callback will receive a JSON string containing a serialized representation of the editor's state. This can be passed to theLoadJSONAsync(string?)
method to allow a user to resume editing where they left off.
The ImageEditor
component supports multiple options for adding custom UI:
-
If you supply
ChildContent
(additional markup nested inside the component, with or without a wrappingChildContent
element), it will be displayed below the existing controls in both edit and preview mode (regardless of theShowEditControls
parameter's value). -
If you supply an
EditContent
child element, it will be displayed when the control is in edit mode (regardless of theShowEditControls
parameter's value). If you also provide an explicitChildContent
element, theChildContent
will be displayed below theEditContent
when in edit mode. -
If you supply a
PreviewContent
child element, it will be displayed when the control is in preview mode (regardless of theShowEditButton
parameter's value). If you also provide an explicitChildContent
element, theChildContent
will be displayed below thePreviewContent
when in preview mode.
Tavenem.Blazor.ImageEditor is currently in a prerelease state. Development is ongoing, and breaking changes are possible before the first production release.
No release date is currently set for v1.0 of Tavenem.Blazor.ImageEditor.
Contributions are always welcome. Please carefully read the contributing document to learn more before submitting issues or pull requests.
Please read the code of conduct before engaging with our community, including but not limited to submitting or replying to an issue or pull request.