Example of Image Editor Integration in ASP.NET MVC Rich Text Editor Control
This example demonstrates how to integrate Image Editor into Rich Text Editor control. To use it, simply click on the image to open the quick toolbar and select Image Editor custom quick toolbar. Then, the image will be opened in the Image Editor.
@* The editor's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "imageEditEditor",
Name = "ImageEdit",
Height = "420px",
EnableImageEditor = true,
InsertImageSettings = new UiRichTextImageSettings { SaveUrl = Url.Action("SaveImage", "RichTextEditorDemo") },
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
Value = (string)ViewData["RTEValue"] + Url.Content("~/Content/images/LumenLogo.png") + "\" alt=\"The Lumen logo\" width=\"260\"></p>"
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "imageEditEditor",
Name = "ImageEdit",
Height = "420px",
EnableImageEditor = true,
InsertImageSettings = new UiRichTextImageSettings { SaveUrl = Url.Action("SaveImage", "RichTextEditorDemo") },
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
Value = (string)ViewData["RTEValue"] + Url.Content("~/Content/images/LumenLogo.png") + "\" alt=\"The Lumen logo\" width=\"260\"></p>"
})
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/ImageEditorIntegration.cshtml; it draws what ViewData holds.
public ActionResult ImageEditorIntegration()
{
ViewData["RTEValue"] = "<p>The picture below can be edited without leaving the editor.</p>" +
"<p align=\"center\"><img src=\"";
ViewData["Tools"] = new List<string> { "Image", "|", "Bold", "Italic", "|", "Alignments", "|", "Undo", "Redo" };
return View();
}
/// <summary>The Image sample's uploads: kept in memory (the 50 newest), as a demo needs no storage of its own.</summary>
private static readonly ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>> Images = new ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>>();
private const int MaxImageBytes = 2000000;
/// <summary>
/// The Image sample's upload action (InsertImageSettings.SaveUrl): a PNG, JPEG, GIF or WebP file of up to 2 MB, known by its first
/// bytes (not its name), answered with { url }. Anything else is refused with 400. The file comes as UploadFiles (the editor's field,
/// as Syncfusion's) or file.
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult SaveImage(HttpPostedFileBase file, HttpPostedFileBase uploadFiles)
{
file = file ?? uploadFiles;
if (file == null || file.ContentLength <= 0 || file.ContentLength > MaxImageBytes)
{
return new HttpStatusCodeResult(400, "An image of up to 2 MB is needed.");
}
var bytes = new byte[file.ContentLength];
int read = 0;
while (read < bytes.Length)
{
int n = file.InputStream.Read(bytes, read, bytes.Length - read);
if (n <= 0) { break; }
read += n;
}
if (read != bytes.Length || ImageType(bytes) == null)
{
return new HttpStatusCodeResult(400, "Only PNG, JPEG, GIF and WebP images are taken.");
}
string id = Guid.NewGuid().ToString("N");
Images[id] = new KeyValuePair<DateTime, byte[]>(DateTime.UtcNow, bytes);
foreach (string old in Images.OrderByDescending(i => i.Value.Key).Skip(50).Select(i => i.Key).ToList()) { KeyValuePair<DateTime, byte[]> gone; Images.TryRemove(old, out gone); }
return Json(new { url = Url.Action("Image", new { id }) });
}
/// <summary>An uploaded image, by the id SaveImage gave it.</summary>
public ActionResult Image(string id)
{
KeyValuePair<DateTime, byte[]> image;
if (id == null || !Images.TryGetValue(id, out image)) { return HttpNotFound(); }
Response.Headers["X-Content-Type-Options"] = "nosniff";
return File(image.Value, ImageType(image.Value));
}
private static string ImageType(byte[] b)
{
if (b.Length > 8 && b[0] == 0x89 && b[1] == 0x50 && b[2] == 0x4E && b[3] == 0x47) { return "image/png"; }
if (b.Length > 3 && b[0] == 0xFF && b[1] == 0xD8 && b[2] == 0xFF) { return "image/jpeg"; }
if (b.Length > 6 && b[0] == 0x47 && b[1] == 0x49 && b[2] == 0x46 && b[3] == 0x38) { return "image/gif"; }
if (b.Length > 12 && b[0] == 0x52 && b[1] == 0x49 && b[2] == 0x46 && b[3] == 0x46 && b[8] == 0x57 && b[9] == 0x45 && b[10] == 0x42 && b[11] == 0x50) { return "image/webp"; }
return null;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Rich Text Editors with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The Image Editor control is integrated into the Rich Text Editor and opens the Image Editor within a Dialog when the Image Editor custom quick toolbar is clicked and allows to edit the image. To achieve this, the sample is customized for the Image Editor and Rich Text Editor
- Include the Image Editor custom toolbar item in the QuickToolbarSettings.Image property of the Rich Text Editor
- Configure Image Editor within a Dialog using the BeforeOpen event of the Dialog
- Open the Dialog on clicking the Image Editor custom toolbar item
- Insert the edited image into the Rich Text Editor by clicking the Insert button