- Supported image formats
- Open an Image
- Save as image
- Events to handle save actions
Contact Support
Open and Save in the ASP.NET CORE Image Editor control
18 Mar 202524 minutes to read
To import an image into the canvas, it must first be converted into a blob object. The Uploader component can be used to facilitate the process of uploading an image from the user interface. Once the image has been uploaded, it can then be converted into a blob and drawn onto the canvas.
To save an edited image in the Image Editor control, use the toBlob method to convert it to a blob object. This will save the image with any annotations or filters that have been applied during the editing process. The saved image can be stored as raw image data or as an image file.
Supported image formats
The Image Editor control supports three common image formats: PNG, JPEG, SVG, and WEBP. These formats allow you to work with a wide range of image files within the Image Editor.
When it comes to saving the edited image, the default file type is set as PNG. This means that when you save the edited image without specifying a different file type, it will be saved as a PNG file. However, it’s important to note that the Image Editor typically provides options or methods to specify a different file type if desired. This allows you to save the edited image in formats other than the default PNG, such as JPEG, SVG, and WEBP, based on your specific requirements or preferences.
Open an Image
The open
method in the Image Editor control offers the capability to open an image by providing it in different formats. This method accepts various types of arguments, such as a base64-encoded string, raw image data, or a hosted/online URL. You can pass either the file name or the actual image data as an argument to the open
method, and it will load the specified image into the Image Editor control. This flexibility allows you to work with images from different sources and formats, making it easier to integrate and manipulate images within the Image Editor control.
Opening local images in the Image Editor
Users can easily open local images in the Image Editor. Simply place the image in the same folder as the sample. By specifying the local file name directly in the open
method, the image will be loaded seamlessly into the editor.
@{
var imageTool = new string[] { };
}
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created" toolbar="@imageTool"></ejs-imageeditor>
</div>
<script>
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Open an image from base64 format
Users can easily open images in the Image Editor using a Base64-encoded string. This method allows you to load images directly from their Base64 representation, ensuring seamless integration and flexibility in your application. Simply pass the Base64 string to the open
method, and the image will be loaded into the editor.
Note:: You can obtain the Base64 representation of an image from the Image Editor using the getImageData
method. This process will be explained in the upcoming section.
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created"></ejs-imageeditor>
</div>
<ejs-button id="savebase64" onclick="savebase64()" cssClass="e-primary" content="SAVE BASE64"></ejs-button>
<ejs-button id="openbase64" onclick="openbase64()" cssClass="e-primary" content="OPEN BASE64"></ejs-button>
<script>
var base64String;
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function savebase64() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
var imageData = imgObj.getImageData();
const canvas = document.createElement('canvas');
canvas.width = imageData.width;
canvas.height = imageData.height;
const context = canvas.getContext('2d');
context.putImageData(imageData, 0, 0);
this.base64String = canvas.toDataURL();
}
function openbase64() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
imgObj.open(this.base64String);
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Open an image from Blob storage
User can easily open images in the Image Editor from Blob storage. This method allows you to load images directly from Blob storage, ensuring seamless integration and flexibility in your application. Simply retrieve the image Blob from storage and pass it to the open
method, and the image will be loaded into the editor.
Note:: You can obtain the Blob URL representation of an image from the Image Editor using the getImageData
method. This process will be explained in the upcoming section.
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created" ></ejs-imageeditor>
</div>
<ejs-button id="saveBlob" onclick="saveBlob()" cssClass="e-primary" content="SAVE BLOB"></ejs-button>
<ejs-button id="openBlob" onclick="openBlob()" cssClass="e-primary" content="OPEN BLOB"></ejs-button>
<script>
var blobUrl;
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function saveBlob() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
var imageData = imgObj.getImageData();
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = imageData.width;
canvas.height = imageData.height;
ctx.putImageData(imageData, 0, 0);
canvas.toBlob((blob) =>{
this.blobUrl = URL.createObjectURL(blob);// For getting blob.
});
}
function openBlob() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
imgObj.open(this.blobUrl);
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Open an image from File Uploader
User can easily open images in the Image Editor using a file uploader. This method allows users to upload an image file from their device and load it directly into the editor. Once the image is selected through the file uploader, pass the file to the open
method, and the image will be seamlessly loaded into the editor.
@{
var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = @Url.Content("https://services.syncfusion.com/aspnet/production/api/FileUploader/Save"), RemoveUrl = @Url.Content("https://services.syncfusion.com/aspnet/production/api/FileUploader/Remove") };
}
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-uploader id="UploadFiles" asyncSettings="@asyncSettings" showFileList="false" selected="selected"></ejs-uploader>
<ejs-imageeditor id="image-editor" created="created"></ejs-imageeditor>
</div>
<script>
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function selected(args) {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (args.filesData.length > 0) {
var reader = new FileReader();
reader.onload = () => {
imageEditorObj.open(reader.result);
};
reader.readAsDataURL(args.filesData[0].rawFile);
}
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Open an image from File Manager
User can easily open images in the Image Editor using the File Manager. This method allows you to browse and select an image file directly from the File Manager and load it into the editor. Once the image is selected, pass the file to the open
method, and the image will be seamlessly loaded into the editor.
<div class="sample-container">
<ejs-filemanager id="flatdata" fileSystemData="ViewBag.ResultData" fileOpen="fileOpen">
</ejs-filemanager>
</div>
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor"></ejs-imageeditor>
</div>
<script>
function fileOpen(args) {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
var file = args.fileDetails;
if (file.isFile && file.imageUrl) {
args.cancel = true;
imageEditorObj.open(file.imageUrl);
}
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.EJ2.Popups;
namespace EJ2CoreSampleBrowser.Controllers
{
public partial class ImageEditorController : Controller
{
public IActionResult ProfilePicture()
{
var permission = new Permission
{
copy = false,
download = false,
write = false,
writeContents = false,
read = true,
upload = false,
message = ""
};
var resultData = new List<FileData>
{
new FileData
{
dateCreated = DateTime.Parse("2023-11-15T19:02:02.3419426+05:30"),
dateModified = DateTime.Parse("2024-01-08T16:55:20.9464164+05:30"),
filterPath = "\\",
hasChild = true,
id = "0",
isFile = false,
name = "Pictures",
parentId = "0",
size = 228465,
type = "folder"
},
new FileData
{
dateCreated = DateTime.Parse("2023-11-15T19:02:02.3419426+05:30"),
dateModified = DateTime.Parse("2024-01-08T16:55:20.9464164+05:30"),
filterPath = "\\",
hasChild = false,
id = "1",
isFile = true,
name = "Flower",
parentId = "0",
size = 65536,
type = ".png",
imageUrl = "https://ej2.syncfusion.com/react/demos/src/image-editor/images/flower.png"
},
new FileData
{
dateCreated = DateTime.Parse("2023-11-15T19:02:02.3419426+05:30"),
dateModified = DateTime.Parse("2024-01-08T16:55:20.9464164+05:30"),
filterPath = "\\",
hasChild = false,
id = "2",
isFile = true,
name = "Bridge",
parentId = "0",
size = 53248,
type = ".png",
imageUrl = "https://ej2.syncfusion.com/react/demos/src/image-editor/images/bridge.png"
}
};
ViewBag.ResultData = resultData;
return View();
}
}
public class Permission
{
public bool copy { get; set; }
public bool download { get; set; }
public bool write { get; set; }
public bool writeContents { get; set; }
public bool read { get; set; }
public bool upload { get; set; }
public string? message { get; set; }
}
public class FileData
{
public DateTime dateCreated { get; set; }
public DateTime dateModified { get; set; }
public string? filterPath { get; set; }
public bool hasChild { get; set; }
public string? id { get; set; }
public bool isFile { get; set; }
public string? name { get; set; }
public string? parentId { get; set; }
public int size { get; set; }
public string? type { get; set; }
public Permission? permission { get; set; }
public string? imageUrl { get; set; }
}
}
Output be like the below.
Open an image from Treeview
Users can open images in the Syncfusion Image Editor by selecting a node from a tree view. When a user clicks on an image node, the corresponding image is loaded into the editor using the open
method. This allows for a seamless image editing experience directly from the TreeView component.
@{
char[] value = { 'C', 'h', 'i', 'l', 'd' };
string Child = new string(value);
}
<div class="control_wrapper">
<ejs-treeview id="treedata" nodeClicked="nodeClicked">
<e-treeview-fields child="Child" dataSource="ViewBag.ParentItems" id="NodeId" parentId="pid" text="NodeText" hasChildren="hasChild" expanded="Expanded"></e-treeview-fields>
</ejs-treeview>
</div>
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor"></ejs-imageeditor>
</div>
<script>
function nodeClicked(args) {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
var treeViewObj = ej.base.getComponent(document.getElementById('treedata'), 'treeview');
let nodeId = args.node.getAttribute('data-uid');
let nodeData = treeViewObj.getTreeData(nodeId)[0];
if (nodeData && nodeData.Image) {
imageEditorObj.open(nodeData.Image);
}
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.EJ2.Popups;
namespace EJ2CoreSampleBrowser.Controllers
{
public partial class ImageEditorController : Controller
{
public IActionResult ProfilePicture()
{
var ParentItems = new List<Parentitem>();
var ChildItem1 = new List<Childitem>();
var ChildItem2 = new List<Childitem>();
var ChildItem3 = new List<Childitem>();
ParentItems.Add(new Parentitem
{
NodeId = "01",
NodeText = "Videos",
Icon = "folder",
Child = ChildItem1,
});
ChildItem1.Add(new Childitem { NodeId = "01-01", NodeText = "Naturals.mp4", Icon = "video" });
ChildItem1.Add(new Childitem { NodeId = "01-02", NodeText = "Wild.mpeg", Icon = "video" });
ParentItems.Add(new Parentitem
{
NodeId = "02",
NodeText = "Documents",
Icon = "folder",
Child = ChildItem2,
});
ChildItem2.Add(new Childitem { NodeId = "02-01", NodeText = "Environment Pollution.docx", Icon = "docx" });
ChildItem2.Add(new Childitem { NodeId = "02-02", NodeText = "Global Water, Sanitation, & Hygiene.docx", Icon = "docx" });
ChildItem2.Add(new Childitem { NodeId = "02-03", NodeText = "Global Warming.ppt", Icon = "ppt" });
ChildItem2.Add(new Childitem { NodeId = "02-04", NodeText = "Social Network.pdf", Icon = "pdf" });
ChildItem2.Add(new Childitem { NodeId = "02-05", NodeText = "Youth Empowerment.pdf", Icon = "pdf" });
ParentItems.Add(new Parentitem
{
NodeId = "03",
NodeText = "Pictures",
Icon = "folder",
Child = ChildItem3,
Expanded = true
});
ChildItem3.Add(new Childitem { NodeId = "03-01", NodeText = "WIN_20160726_094117.JPG", Image = "https://ej2.syncfusion.com/react/demos/src/image-editor/images/bridge.png" });
ChildItem3.Add(new Childitem { NodeId = "03-02", NodeText = "WIN_20160726_094118.JPG", Image = "https://ej2.syncfusion.com/react/demos/src/image-editor/images/flower.png" });
ViewBag.ParentItems = ParentItems;
return View();
}
}
public class Parentitem
{
public string NodeId;
public string NodeText;
public string Image;
public string Icon;
public bool Expanded;
public bool Selected;
public List<Childitem> Child;
}
public class Childitem
{
public string NodeId;
public string NodeText;
public string Image;
public string Icon;
public bool Expanded;
public bool Selected;
public List<SubChilditem> Child;
}
public class SubChilditem
{
public string NodeId;
public string NodeText;
public string Image;
public string Icon;
public bool Expanded;
public bool Selected;
}
}
Output be like the below.
Add watermarks while opening an image
You can utilize the FileOpened
event, which triggers once the image is loaded into the image editor. After this event, you can use the ‘drawText’ method to add a watermark. This approach allows the watermark to be automatically drawn on the canvas every time an image is opened in the editor, making it useful for handling copyright-related content.
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created" fileOpened = "fileOpened"></ejs-imageeditor>
</div>
<script>
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function fileOpened() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
imgObj.drawText(dimension.x, dimension.y, 'Syncfusion', 'Arial', 40, false, false, '#80330075');
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Save as image
The export
method in the Image Editor control enables you to save the modified image as a file on the local device. This method accepts two parameters: the file name and the file type.
By providing a file name, you can specify the desired name for the saved image file. Additionally, you can also specify the quality and file type to determine the format in which the image should be saved. This allows you to save the image according to your specific requirements or preferences, such as PNG, JPEG, SVG, and WEBP.
By utilizing the export
method with the appropriate file name and file type, you can conveniently save the modified image as a file on the local device, ensuring that it is easily accessible and shareable.
In the following example, the export
method is used in the button click event.
@{
var imageTool = new string[] { };
}
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created" toolbar="@imageTool"></ejs-imageeditor>
</div>
<ejs-button id="btnClick" onclick="clickHandler()" cssClass="e-primary" content="Click"></ejs-button>
<script>
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function clickHandler() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
imageEditorObj.export("PNG", "Syncfusion"); // File type, file name
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
List<object> items = new List<object>();
items.Add(new
{
text = "Png"
});
items.Add(new
{
text = "Jpeg"
});
items.Add(new
{
text = "Svg"
});
ViewBag.datasource = items;
return View();
}
Output be like the below.
Save the image as base64 format
To save an image as a base64 format, use the getImageData
method of the editor to retrieve the image data and convert it into a Data URL, which contains the base64-encoded string. By invoking the open
method on the Syncfusion® Image Editor instance, you can load this Data URL into the editor. The resulting base64 string can then be embedded directly in HTML or CSS or transmitted over data channels without requiring an external file.
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created"></ejs-imageeditor>
</div>
<ejs-button id="zoomInClick" onclick="saveImage()" cssClass="e-primary" content="Save Image"></ejs-button>
<script>
var base64String;
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function saveImage() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
var imageData = imgObj.getImageData();
var canvas = document.createElement('canvas');
canvas.width = imageData.width;
canvas.height = imageData.height;
// Get the 2D rendering context of the canvas
const context = canvas.getContext('2d');
// Put the ImageData onto the canvas
context.putImageData(imageData, 0, 0);
// Convert the canvas content to a Base64 encoded URL
this.base64String = canvas.toDataURL();
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Save the image as byte[]
To save an image as a byte array (byte[]), use the getImageData
method of the editor to retrieve the image data and convert it into a byte array. You can then invoke the open
method on the Syncfusion® Image Editor instance to load this byte array into the editor. The resulting byte array can be stored in a database for data management and maintenance.
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created"></ejs-imageeditor>
</div>
<div class="mt-3">
<button id="saveImage" class="e-btn e-primary">Save Byte[]</button>
<button id="loadImage" class="e-btn e-secondary">Load Byte[]</button>
</div>
<script>
var imageEditorObj;
var byteArray;
function created() {
imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
document.getElementById('saveImage').onclick = function () {
var imageData = imageEditorObj.getImageData();
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
if (!ctx) return;
canvas.width = imageData.width;
canvas.height = imageData.height;
ctx.putImageData(imageData, 0, 0);
canvas.toBlob((blob) => {
if (blob) {
let reader = new FileReader();
reader.readAsArrayBuffer(blob);
reader.onloadend = function () {
byteArray = new Uint8Array(reader.result);
};
}
}, 'image/png');
};
document.getElementById('loadImage').onclick = function () {
if (byteArray) {
let blob = new Blob([byteArray], { type: 'image/png' });
let url = URL.createObjectURL(blob);
imageEditorObj.open(url);
}
};
</script>
<style>
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Save the image as Blob
To save an image as a blob, use the getImageData
method of the editor to retrieve the image data and convert it into a blob. You can then invoke the open
method on the Syncfusion® Image Editor instance to load this byte array into the editor. The resulting byte array can be stored in a database for data management and maintenance.
@{
var imageTool = new string[] { };
}
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created" toolbar="@imageTool"></ejs-imageeditor>
</div>
<ejs-button id="saveBlob" onclick="saveBlob()" cssClass="e-primary" content="Save Blob"></ejs-button>
<script>
var blobUrl;
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function saveBlob() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
var imageData = imgObj.getImageData();
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = imageData.width;
canvas.height = imageData.height;
ctx.putImageData(imageData, 0, 0);
canvas.toBlob((blob) =>{
this.blobUrl = URL.createObjectURL(blob);// For getting blob.
});
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Add watermarks while saving the image
You can utilize the FileOpened
event, which triggers once the image is loaded into the image editor. After this event, you can use the drawText
method to add a watermark. This approach allows the watermark to be automatically drawn on the canvas every time an image is opened in the editor, making it useful for handling copyright-related content.
@{
var imageTool = new string[] { };
}
<div class="col-lg-12 control-section e-img-editor-sample">
<ejs-imageeditor id="image-editor" created="created" beforeSave = "beforeSaved" saved="saved"></ejs-imageeditor>
</div>
<script>
function created() {
var imageEditorObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
if (ej.base.Browser.isDevice) {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/flower.png');
} else {
imageEditorObj.open('https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png');
}
}
function beforeSaved() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
imgObj.drawText(dimension.x, dimension.y, 'Syncfusion', 'Arial', 40, false, false, '#80330075');
}
function saved() {
var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
imgObj.deleteShape(shapes[shapes.length - 1].id);
}
</script>
<style>
.image-editor {
margin: 0 auto;
}
.e-img-editor-sample {
height: 80vh;
width: 100%;
}
@@media only screen and (max-width: 700px) {
.e-img-editor-sample {
height: 75vh;
width: 100%;
}
}
.control-wrapper {
height: 100%;
}
</style>
public ActionResult Default()
{
return View();
}
Output be like the below.
Remove default Save button and add custom button to save the image to server
User can leverage the Toolbar
property to replace the default save button with a custom one. By doing so, you can use the getImageData
method to retrieve the image data, convert it to base64 format, and then save it to the server. This approach gives you more control over the image-saving process.
Prevent default save option and save the image to specific location
User can make use of the BeforeSave
event, which triggers just before the image is downloaded, to override the default save option by setting args.cancel
to true. Afterward, you can utilize the getImageData method to retrieve the current image data and convert it into a format like byte[], blob, or base64 for further processing. This gives you greater flexibility in handling the image data.
Events to handle save actions
The Image Editor provides several events related to opening and saving images. These events offer detailed control over the image handling process. For comprehensive information about these events, including their triggers and usage, please refer to the dedicated section on open and save support. This section will provide you with the specifics needed to effectively utilize these events in your application.
File opened event
The FileOpened
event is triggered in the Image Editor control after an image is successfully loaded. It provides the FileOpenEventArgs
as the event argument, which contains two specific arguments:
-
FileName: This argument is a string that contains the file name of the opened image. It represents the name of the file that was selected or provided when loading the image into the Image Editor.
-
FileType: This argument is a string that contains the type of the opened image. It specifies the format or file type of the image that was loaded, such as PNG, JPEG, SVG, and WEBP.
By accessing these arguments within the FileOpened event handler, you can retrieve information about the loaded image, such as its file name and file type. This can be useful for performing additional actions or implementing logic based on the specific image that was opened in the Image Editor control.
Saving event
The saving
event is triggered in the Image Editor control when an image is being saved to the local disk. It provides the SaveEventArgs
as the event argument, which includes the following specific arguments:
-
FileName: This argument is a string that holds the file name of the saved image. It represents the name of the file that will be used when saving the image to the local disk.
-
FileType: This argument is a string indicating the type or format of the saved image. It specifies the desired file type in which the image will be saved, such as PNG, JPEG, SVG, and WEBP.
-
Cancel: This argument is a boolean value that can be set to true in order to cancel the saving action. By default, it is set to false, allowing the saving process to proceed. However, if you want to prevent the saving action from occurring, you can set Cancel to true within the event handler.
By accessing these arguments within the Saving event handler, you can retrieve information about the file name and file type of the image being saved. Additionally, you have the option to cancel the saving action if necessary.
Created event
The created
event is triggered once the Image Editor control is created. This event serves as a notification that the component has been fully initialized and is ready to be used. It provides a convenient opportunity to render the Image Editor with a predefined set of initial settings, including the image, annotations, and transformations.
Destroyed event
The destroyed
event is triggered once the Image Editor control is destroyed or removed from the application. This event serves as a notification that the component and its associated resources have been successfully cleaned up and are no longer active.