Overview
The .NET MAUI RichTextEditor control allows you to add (insert), cut, copy, paste, resize, and delete images by using built-in toolbar items.
You can add images in the RichTextEditor by using the predefined toolbar items:
RichTextEditorAddImageToolbarItem—Allows you to add an image—the RichTextEditorPickImageevent is fired, so you can handle the logic for selecting an image. The toolbar is enabled if no image is selected.RichTextEditorAddOrEditImageToolbarItem—Adds or edits images depending whether image is selected. If image is selected, the editor opens a dialog to edit the image. If image is not selected, thePickImageevent fires.RichTextEditorImageNavigationToolbarItem—Adds images or navigates to additional toolbar items depending whether image is selected. If image is selected, the editor navigates to image operation toolbar items. If image is not selected, thePickImageevent fires.
You can insert images from URI, Data(byte []), Stream, File. The image source is of type RichTextImageSource.
RichTextImageSource FromData(byte[] data, RichTextImageType type)RichTextImageSource FromFile(string path)RichTextImageSource FromFile(string path, RichTextImageType type)RichTextImageSource FromStream(Stream stream, RichTextImageType type)RichTextImageSource FromStream(Func<Stream> stream, RichTextImageType type)RichTextImageSource FromStream(Func<Task<Stream>> stream, RichTextImageType type)RichTextImageSource FromStream(Func<CancellationToken, Task<Stream>> stream, RichTextImageType type)RichTextImageSource FromUrl(string uri)
Also you have to point out the image format type. The supported image format types(of type RichTextImageType) are:
GifJpegPngSvgWebp
Required Permissions
If you want to work with images from the device gallery, then you have to grant the required permissions to the RichTextEditor.
To enable the users to add images by using the predefined toolbar items, use RichTextEditorAddOrEditImageToolbarItem on desktop and RichTextEditorImageNavigationToolbarItem on mobile platforms. Both toolbar items provide a way to pick an image directly. The RichTextEditor PickImage event is fired. You need to manually implement the logic for selecting an image inside the PickImage event handler.
private void OnPickImage(object sender, Telerik.Maui.Controls.RichTextEditor.PickImageEventArgs e)
{
// add your custom logic here
}
Events
-
PickImage—Raised when the user has requested to pick an image in theRadRichTextEditor. ThePickImageevent handler receives two parameters:- The
sender, which is the RichTextEditor control; PickImageEventArgsprovides the following methods:Accept—Invoke this method when the user has picked an image. Receives one parameterimagesourceof typeRichTextImageSource(Specifies theRichTextImageSourcefor the picked image);Cancel—Invoke this method when the user has canceled the operation;
- The
-
InsertImageError—Raised when trying to insert an image in theRadRichTextEditor. TheInsertImageErrorevent handler receives two parameters:- The
sender, which is the RichTextEditor control; InsertImageErrorEventArgsprovides the following methods:Source—Of typeRichTextImageSource. The property allows you to get the source of the image (read-only property).Error—Of typeException. Specifies the exception that is raised when image cannot be inserted.
- The
-
IsImageSelectedChanged—Raised when an image inside the editor is selected. TheIsImageSelectedChangedevent handler receives two parameters:- The
sender, which is the RichTextEditor control; RadValueChangedEventArgs<bool>, which provides theNewValueandPreviousValueproperties of typeTValue.
- The
Methods
- The
GetImageAsyncmethod asynchronously returns an object of typeRichTextImagewhich represents the current selected image (ornullif no image is available). TheRichTextImageobject contains the source, title, width, height of the image.
Commands
InsertImageCommand(ICommand)—Used to insert images in the editor. The command takes a single parameter of typeRichTextImage. TheRichTextImageobject contains the source, title, width, height of the image. If you do not set width and height explicitly, the image will be added with its original size.RemoveImageCommand(ICommand)—Uses for removing images from the editor.