Open and Save in React Image Editor

03 Sep 202624 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 React Image Editor component, use the getImageData method (or the export API for direct file download) to retrieve the edited image as a base64 string, Blob, or byte array. This will include any annotations or filters that have been applied during the editing process.

Supported image formats

The React Image Editor control supports five common image formats: PNG, JPEG, SVG, WEBP, and BMP. These formats allow you to work with a wide range of image files within the React 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 React 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, or WEBP, based on your specific requirements or preferences.

Open an image

The open method in the React 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 React 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 React Image Editor control.

Opening local images in the React Image Editor

Users can easily open local images in the React 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.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} created={this.imageEditorCreated.bind(this)} height="350px" toolbar = {[]}>
            </ImageEditorComponent>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} toolbar = {[]}>
        </ImageEditorComponent>
            </div>
    );
}
export default App;

ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Open an image from base64 format

Users can open images in the React Image Editor using a Base64-encoded string. 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 React Image Editor using the getImageData method.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    base64String;
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    setImage() {
        if (this.base64String) {
            this.imgObj.open(this.base64String);
        }
    }
    saveImage(args) {
        let imageData = this.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();
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)}>
            </ImageEditorComponent>
                    <div>
                        <ButtonComponent cssClass='e-primary' content='Set Image' onClick={this.setImage.bind(this)}/>
                        <ButtonComponent cssClass='e-primary' content='Save Image' onClick={this.saveImage.bind(this)}/>
                    </div>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    let base64String: any;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function setImage(): void {
        if (base64String) {
            imgObj.open(base64String);
        }
    }

    function saveImage(args: any): void {
        let imageData = imgObj.getImageData();
        const canvas = document.createElement('canvas');
        canvas.width = imageData.width;
        canvas.height = imageData.height;
        // Get the 2D rendering context of the canvas
        const context: any = canvas.getContext('2d');
        // Put the ImageData onto the canvas
        context.putImageData(imageData, 0, 0);
        // Convert the canvas content to a Base64 encoded URL
        base64String = canvas.toDataURL();
    }

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated}>
        </ImageEditorComponent>
                <div>
                    <ButtonComponent cssClass='e-primary' content='Save Image' onClick = {saveImage}/>
                    <ButtonComponent cssClass='e-primary' content='Load base64 Image' onClick = {setImage}/>
                </div>
            </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Open an image from Blob storage

Users can open images in the React Image Editor from Blob storage. Retrieve the image Blob from storage and pass it to the open method to load the image into the editor.

Note: You can obtain the Blob representation of an image from the React Image Editor using the getImageData method.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser, getComponent } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    blobUrl = '';
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    getBlob() {
        let imageData = this.imgObj.getImageData();
        let canvas = document.createElement('canvas');
        let 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.
        });
    }
    btnClick(url) {
        this.imgObj.open(this.blobUrl);
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent id="image-editor" ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)}>
            </ImageEditorComponent>
                    <div>
                        <ButtonComponent cssClass='e-primary' content='Save' onClick={this.getBlob.bind(this)}/>
                        <ButtonComponent cssClass='e-primary' content='Open Blob' onClick = {this.btnClick.bind(this)}/>
                    </div>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser, getComponent } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    let blobUrl: any;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function getBlob(): void {
        let imageData = imgObj.getImageData();
        let canvas = document.createElement('canvas');
        let ctx: any = canvas.getContext('2d');
        canvas.width = imageData.width;
        canvas.height = imageData.height;
        ctx.putImageData(imageData, 0, 0);
        canvas.toBlob((blob) => {
            blobUrl = URL.createObjectURL(blob as any);// For getting blob.
        });
    }

    function btnClick(): void {
        imgObj.open(blobUrl);
    }

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent id="image-editor" ref={(img) => { imgObj = img }} created={imageEditorCreated}>
        </ImageEditorComponent>
                <div>
                    <ButtonComponent cssClass='e-primary' content='Save' onClick = {getBlob}/>
                    <ButtonComponent cssClass='e-primary' content='Open Blob' onClick = {btnClick}/>
                </div>
            </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Open an image from File Uploader

Users can open images in the React Image Editor using a file uploader. Once the image is selected through the file uploader, pass the File/Blob to the open method to load it into the editor.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { UploaderComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;

    selected(args) {
        if (args.filesData.length > 0) {
            const reader = new FileReader();
            reader.onload = () => {
               this.imgObj.open(reader.result);
            };
            reader.readAsDataURL(args.filesData[0].rawFile);
        }
    }
    
    render() {
        return (<div className='e-img-editor-sample'>
            <UploaderComponent selected={this.selected.bind(this)} showFileList={false}></UploaderComponent>
            <ImageEditorComponent height='350px' ref={(img) => { this.imgObj = img; }}>
            </ImageEditorComponent>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { UploaderComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    
    function selected(args: any): void {
        if (args.filesData.length > 0) {
            const reader = new FileReader();
            reader.onload = () => {
               imgObj.open(reader.result);
            };
            reader.readAsDataURL(args.filesData[0].rawFile);
        }
    }

    return (
        <div className='e-img-editor-sample'>
            <UploaderComponent selected={selected} showFileList={false}></UploaderComponent>
            <ImageEditorComponent height='350px' ref={(img) => { imgObj = img }}>
            </ImageEditorComponent>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Open an image from File Manager

Users can open images in the React Image Editor using the File Manager. Browse and select an image file, then pass the File/Blob to the open method to load it into the editor.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { FileManagerComponent, FileData } from '@syncfusion/ej2-react-filemanager';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    resultData = [
        {
            dateCreated: new Date("2023-11-15T19:02:02.3419426+05:30"),
            dateModified: new Date("2024-01-08T16:55:20.9464164+05:30"),
            filterPath: "\\",
            hasChild: true,
            id: "0",
            isFile: false,
            name: "Pictures",
            parentId: "0",
            size: 228465,
            type: "folder"
        },
        {
            dateCreated: new Date("2023-11-15T19:02:02.3419426+05:30"),
            dateModified: new Date("2024-01-08T16:55:20.9464164+05:30"),
            filterPath: "\\Pictures\\",
            hasChild: false,
            id: '1',
            isFile: true,
            name: "Flower",
            parentId: '0',
            size: 69632,
            type: ".png",
            imageUrl: "https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg"
        },
        {
            dateCreated: new Date("2023-11-15T19:02:02.3419426+05:30"),
            dateModified: new Date("2024-01-08T16:55:20.9464164+05:30"),
            filterPath: "\\Pictures\\",
            hasChild: false,
            id: '2',
            isFile: true,
            name: "Bridge",
            parentId: '0',
            size: 48951,
            type: ".png",
            imageUrl: "https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg"
        }
    ];
    fileOpen(args) {
        const file = args.fileDetails;
        if (file.isFile && file.imageUrl) {
            args.cancel = true;
            this.imgObj?.open(file.imageUrl);
        }
    }

    render() {
        return (
            <div className="e-img-editor-sample">
                <FileManagerComponent id="overview_file" fileSystemData={this.resultData} fileOpen={this.fileOpen.bind(this)} height="200px"/>
                <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" />
            </div>
        );
    }
}

ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { FileManagerComponent, FileData } from '@syncfusion/ej2-react-filemanager';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    let imgObj: ImageEditorComponent;
    const resultData: FileData[] = [
        {
            dateCreated: new Date("2023-11-15T19:02:02.3419426+05:30"),
            dateModified: new Date("2024-01-08T16:55:20.9464164+05:30"),
            filterPath: "\\",
            hasChild: true,
            id: "0",
            isFile: false,
            name: "Pictures",
            parentId: "0",
            size: 228465,
            type: "folder"
        },
        {
            dateCreated: new Date("2023-11-15T19:02:02.3419426+05:30"),
            dateModified: new Date("2024-01-08T16:55:20.9464164+05:30"),
            filterPath: "\\Pictures\\",
            hasChild: false,
            id: '1',
            isFile: true,
            name: "Flower",
            parentId: '0',
            size: 69632,
            type: ".png",
            imageUrl: "https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg"
        },
        {
            dateCreated: new Date("2023-11-15T19:02:02.3419426+05:30"),
            dateModified: new Date("2024-01-08T16:55:20.9464164+05:30"),
            filterPath: "\\Pictures\\",
            hasChild: false,
            id: '2',
            isFile: true,
            name: "Bridge",
            parentId: '0',
            size: 48951,
            type: ".png",
            imageUrl: "https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg"
        }
    ];
    const fileOpen = (args: { fileDetails: FileData, cancel: boolean }) => {
        const file = args.fileDetails;
        if (file.isFile && file.imageUrl) {
            args.cancel = true;
            imgObj.open(file.imageUrl);
        }
    };

    return (
        <div className="e-img-editor-sample">
            <FileManagerComponent id="overview_file" fileSystemData={resultData} fileOpen={fileOpen} height="200px"/>
            <ImageEditorComponent ref={(img) => { imgObj = img }} />
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Open an image from Treeview

Users can open images in the Syncfusion React 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.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { TreeViewComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    treeView;
    data = [
        {
            "nodeId": "01", "nodeText": "Videos", "icon": "folder",
            "nodeChild": [
                { "nodeId": "01-01", "nodeText": "Naturals.mp4", "icon": "video" },
                { "nodeId": "01-02", "nodeText": "Wild.mpeg", "icon": "video" }
            ]
        },
        {
            "nodeId": "02", "nodeText": "Documents", "icon": "folder",
            "nodeChild": [
                { "nodeId": "02-01", "nodeText": "Environment Pollution.docx", "icon": "docx" },
                { "nodeId": "02-02", "nodeText": "Global Water, Sanitation, & Hygiene.docx", "icon": "docx" },
                { "nodeId": "02-03", "nodeText": "Global Warming.ppt", "icon": "ppt" },
                { "nodeId": "02-04", "nodeText": "Social Network.pdf", "icon": "pdf" },
                { "nodeId": "02-05", "nodeText": "Youth Empowerment.pdf", "icon": "pdf" }
            ]
        },
        { 
            "nodeId": "03", "nodeText": "Pictures", "icon": "folder", "expanded": true,
            "nodeChild": [
            {
                "nodeId": "03-01", "nodeText": "Camera Roll", "icon": "folder", "expanded": true,
                "nodeChild": [
                    { "nodeId": "03-01-01", "nodeText": "Flower", "image": "https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg" },
                    { "nodeId": "03-01-02", "nodeText": "Bridge", "image": "https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg" }
                ]
            },
        ]}
    ];
    fields = { dataSource: this.data, id: 'nodeId', text: 'nodeText', child: 'nodeChild' };
    clicked(args) {
        let nodeId = args.node.getAttribute('data-uid');
        let nodeData = this.treeView.getTreeData(nodeId)[0];
        if (nodeData && nodeData.image) {
            this.imgObj.open(nodeData.image);
        }
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <TreeViewComponent ref={(tree) => { this.treeView = tree }} fields={this.fields} nodeClicked={this.clicked.bind(this)} />
            <ImageEditorComponent ref={(img) => { this.imgObj = img }} height="350px">
            </ImageEditorComponent>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { TreeViewComponent } from '@syncfusion/ej2-react-navigations';
import { Browser, getComponent } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    let treeView: TreeViewComponent;
    let data = [
        {
            "nodeId": "01", "nodeText": "Videos", "icon": "folder",
            "nodeChild": [
                { "nodeId": "01-01", "nodeText": "Naturals.mp4", "icon": "video" },
                { "nodeId": "01-02", "nodeText": "Wild.mpeg", "icon": "video" }
            ]
        },
        {
            "nodeId": "02", "nodeText": "Documents", "icon": "folder",
            "nodeChild": [
                { "nodeId": "02-01", "nodeText": "Environment Pollution.docx", "icon": "docx" },
                { "nodeId": "02-02", "nodeText": "Global Water, Sanitation, & Hygiene.docx", "icon": "docx" },
                { "nodeId": "02-03", "nodeText": "Global Warming.ppt", "icon": "ppt" },
                { "nodeId": "02-04", "nodeText": "Social Network.pdf", "icon": "pdf" },
                { "nodeId": "02-05", "nodeText": "Youth Empowerment.pdf", "icon": "pdf" }
            ]
        },
        { 
            "nodeId": "03", "nodeText": "Pictures", "icon": "folder", "expanded": true,
            "nodeChild": [
            {
                "nodeId": "03-01", "nodeText": "Camera Roll", "icon": "folder", "expanded": true,
                "nodeChild": [
                    { "nodeId": "03-01-01", "nodeText": "Flower", "image": "https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg" },
                    { "nodeId": "03-01-02", "nodeText": "Bridge", "image": "https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg" }
                ]
            },
        ]}
    ];
    const fields = { dataSource: data, id: 'nodeId', text: 'nodeText', child: 'nodeChild' };
    function clicked(args: any) {
        let nodeId = args.node.getAttribute('data-uid');
        let nodeData = treeView.getTreeData(nodeId)[0];
        if (nodeData && nodeData.image) {
            imgObj.open(nodeData.image);
        }
    }

    return (
        <div className='e-img-editor-sample'>
            <TreeViewComponent ref={(tree) => { treeView = tree }} fields={fields} nodeClicked={clicked} />
            <ImageEditorComponent ref={(img) => { imgObj = img }}>
            </ImageEditorComponent>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Add watermarks while opening an image

You can utilize the fileOpened event, which triggers once the image is loaded into the React Image Editor. After this event, you can use the drawText method to add a watermark.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    fileOpened() {
        let dimension = this.imgObj.getImageDimension();
        this.imgObj.drawText(dimension.x, dimension.y, 'Syncfusion', 'Arial', 40, false, false, '#80330075');
    };
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)} fileOpened = {this.fileOpened.bind(this)}>
            </ImageEditorComponent>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function fileOpened(): void {
        let dimension: any = imgObj.getImageDimension();
        imgObj.drawText(dimension.x, dimension.y, 'Syncfusion', 'Arial', 40, false, false, '#80330075');
    };

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} fileOpened = {fileOpened}>
        </ImageEditorComponent>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Opening Images with Custom Width and Height

Users can now open images with specific width and height values using the imageSettings parameter in the open method. This enhancement introduces three additional properties: width, height, and isAspectRatio. These options allow precise control over the image dimensions, with the flexibility to preserve the original aspect ratio if needed. This feature is especially useful when rendering high-resolution images or when fitting images into fixed-size layouts or canvas areas.

The following behaviors are supported through these properties:

  • Contains behavior: By specifying only one dimension (either width or height) and enabling isAspectRatio, the other dimension is automatically calculated to maintain the image’s original proportions.
  • Cover behavior: When both width and height are specified with isAspectRatio set to true, the image scales proportionally to fit within the given dimensions while preserving its aspect ratio.
  • Stretch or Shrink behavior: Setting isAspectRatio to false forces the image to strictly follow the specified width and height, allowing it to stretch or shrink regardless of its original aspect ratio.

The following example showcases how all three behaviors can be achieved using the open method.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";


export default class App extends React.Component {
    imgObj;
    imageEditorCreated() {
        this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
    }

    contains() {
        this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg', true, { backgroundColor: '', width: 550, height: null, isAspectRatio : true  });
    }
    cover() {
        this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg', true, { backgroundColor: '', width: 550, height: 550, isAspectRatio : true  });
    }
    stretch() {
        this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg', true, { backgroundColor: '', width: 330, height: 330, isAspectRatio : false });
    }

    render() {
        return (<div className='e-img-editor-sample'><ImageEditorComponent ref={(img) => { this.imgObj = img; }} width="550px" height="330px" created={this.imageEditorCreated.bind(this)} toolbar={[]} />
            <div style=>
                <ButtonComponent cssClass='e-primary' content='Fit to Width (Aspect Ratio)' onClick={this.contains.bind(this)} />
                <ButtonComponent cssClass='e-primary' content='Cover (Aspect Ratio)' onClick={this.cover.bind(this)} />
                <ButtonComponent cssClass='e-primary' content='Stretch / Shrink' onClick={this.stretch.bind(this)} />
            </div>
        </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    function imageEditorCreated(): void {
        imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
    }
    function contains(): void {
        imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg', true, { backgroundColor: '', width: 550, height: null, isAspectRatio : true });
    }
    function cover(): void {
        imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg', true, { backgroundColor: '', width: 550, height: 550, isAspectRatio : true });
    }
    function stretch(): void {
        imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg', true, { backgroundColor: '', width: 330, height: 330, isAspectRatio : false });
    }

    return (<div className='e-img-editor-sample'><ImageEditorComponent ref={(img) => { imgObj = img }} width="550px" height="330px" created={imageEditorCreated} toolbar={[]} />
            <div style=>
                <ButtonComponent cssClass='e-primary' content='Fit to Width (Aspect Ratio)' onClick={contains} />
                <ButtonComponent cssClass='e-primary' content='Cover (Aspect Ratio)' onClick={cover} />
                <ButtonComponent cssClass='e-primary' content='Stretch / Shrink' onClick={stretch} />
            </div>
        </div>);
}
export default App;

ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Export (Save)

The export method is used to save the modified image as a file; it accepts a file name and file type as parameters. The file type parameter supports PNG, JPEG, SVG, and WEBP; the default file type is PNG. Users can specify file name, file type, and image quality when exporting.

In the following example, the export method is used in the button click event.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    save() {
        this.imgObj.export("PNG", "Syncfusion"); // File type, file name
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)} toolbar={[]}></ImageEditorComponent>
            <div>
                <ButtonComponent cssClass='e-primary' content='Save' onClick={this.save.bind(this)} />
            </div>
        </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function save(): void {
        imgObj.export("PNG", "Syncfusion"); // File type, file name
    }

    return (
        <div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} toolbar={[]}></ImageEditorComponent>
            <div>
                <ButtonComponent cssClass='e-primary' content='Save' onClick={save} />
            </div>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Save the image as base64 format

To save an image as a base64 string, use the getImageData method of the editor to retrieve the image data as a Data URL (base64-encoded string).

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser, isNullOrUndefined } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    base64String
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    saveImage() {
        let imageData = this.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();
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)}></ImageEditorComponent>
            <div>
                <ButtonComponent cssClass='e-primary' content='Save as Base64' onClick={this.saveImage.bind(this)} />
            </div>
        </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser, isNullOrUndefined } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    let base64String: any;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }

    function saveImage(): void {
        let imageData = imgObj.getImageData();
        const canvas = document.createElement('canvas');
        canvas.width = imageData.width;
        canvas.height = imageData.height;
        const context: any = canvas.getContext('2d');
        context.putImageData(imageData, 0, 0);
        base64String = canvas.toDataURL();
    }

    return (
        <div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated}></ImageEditorComponent>
            <div>
                <ButtonComponent cssClass='e-primary' content='Save as Base64' onClick={saveImage} />
            </div>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Save the image as byte[]

To save an image as a byte array, use the getImageData method and convert the returned data to the desired byte[] representation for storage.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    byteArray;
    imageEditorCreated = () => {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    };
    saveImage = () => {
        const imageData = this.imgObj.getImageData();
        const canvas = document.createElement('canvas');
        const 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) {
                const reader = new FileReader();
                reader.readAsArrayBuffer(blob);
                reader.onloadend = () => {
                    this.byteArray = new Uint8Array(reader.result);
                };
            }
        }, 'image/png');
    };
    loadImage = () => {
        if (this.byteArray) {
            const blob = new Blob([this.byteArray], { type: 'image/png' });
            const url = URL.createObjectURL(blob);
            this.imgObj.open(url);
        }
    };

    render() {
        return (
            <div className='e-img-editor-sample'>
                <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated}></ImageEditorComponent>
                <div>
                    <ButtonComponent cssClass='e-primary' content='Save Byte[]' onClick={this.saveImage} />
                    <ButtonComponent cssClass='e-primary' content='Load Byte[]' onClick={this.loadImage} />
                </div>
            </div>
        );
    }
}

ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    let byteArray: any;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function saveImage(): void {
        const imageData = imgObj.getImageData();
        const canvas = document.createElement('canvas');
        const 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) {
                const reader = new FileReader();
                reader.readAsArrayBuffer(blob);
                reader.onloadend = () => {
                    byteArray = new Uint8Array(reader.result as ArrayBuffer);
                };
            }
        }, 'image/png');
    }
    function loadImage(): void {
        if (byteArray) {
            const blob = new Blob([byteArray], { type: 'image/png' });
            const url = URL.createObjectURL(blob);
            imgObj.open(url);
        }
    }

    return (
        <div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated}></ImageEditorComponent>
            <div>
                <ButtonComponent cssClass='e-primary' content='Save Image' onClick={saveImage} />
                <ButtonComponent cssClass='e-primary' content='Load Image' onClick={loadImage} />
            </div>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Save the image as Blob

To save an image as a Blob, use the getImageData method to retrieve a Blob representation of the edited image.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser, getComponent } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    blobUrl = '';
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    saveBlob(args) {
        let imageData = this.imgObj.getImageData();
        let canvas = document.createElement('canvas');
        let 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.
        });
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)}>
            </ImageEditorComponent>
                    <div>
                        <ButtonComponent cssClass='e-primary' content='Save Blob' onClick={this.saveBlob.bind(this)}/>
                    </div>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser, getComponent } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    let blobUrl: any;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }

    function saveBlob(args: any): void {
        let imageData = imgObj.getImageData();
        let canvas = document.createElement('canvas');
        let ctx: any = canvas.getContext('2d');
        canvas.width = imageData.width;
        canvas.height = imageData.height;
        ctx.putImageData(imageData, 0, 0);
        canvas.toBlob((blob) => {
            blobUrl = URL.createObjectURL(blob);// For getting blob.
        });
    }

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated}>
        </ImageEditorComponent>
                <div>
                    <ButtonComponent cssClass='e-primary' content='Save Blob' onClick = {saveBlob}/>
                </div>
            </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Add watermarks while saving the image

You can utilize the fileOpened event, which triggers once the image is loaded into the React Image Editor. After this event, you can use the drawText method to add a watermark.

import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

export default class App extends React.Component {
    imgObj;
    imageEditorCreated() {
        if (Browser.isDevice) {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        }
        else {
            this.imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    beforeSaved() {
        let dimension = this.imgObj.getImageDimension();
        this.imgObj.drawText(dimension.x, dimension.y, 'Syncfusion', 'Arial', 40, false, false, '#80330075');
    };
    saved() {
        let shapes = this.imgObj.getShapeSettings();
        this.imgObj.deleteShape(shapes[shapes.length - 1].id);
    }
    render() {
        return (<div className='e-img-editor-sample'>
            <ImageEditorComponent ref={(img) => { this.imgObj = img; }} height="350px" created={this.imageEditorCreated.bind(this)} beforeSave = {this.beforeSaved.bind(this)} saved = {this.saved.bind(this)}>
            </ImageEditorComponent>
                </div>);
    }
}
ReactDOM.render(<App />, document.getElementById('image-editor'));
import { ImageEditorComponent } from '@syncfusion/ej2-react-image-editor';
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { Browser } from '@syncfusion/ej2-base';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    let imgObj: ImageEditorComponent;
    function imageEditorCreated(): void {
        if (Browser.isDevice) {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/flower.jpeg');
        } else {
            imgObj.open('https://ej2.syncfusion.com/react/documentation/image-editor/images/bridge.jpeg');
        }
    }
    function beforeSaved(): void {
        let dimension: any = imgObj.getImageDimension();
        imgObj.drawText(dimension.x, dimension.y, 'Syncfusion', 'Arial', 40, false, false, '#80330075');
    };
    function saved(): void {
        let shapes = imgObj.getShapeSettings();
        imgObj.deleteShape(shapes[shapes.length - 1].id);
    }

    return (
        <div className='e-img-editor-sample'>
        <ImageEditorComponent ref={(img) => { imgObj = img }} created={imageEditorCreated} beforeSave = {beforeSaved} saved = {saved}>
        </ImageEditorComponent>
        </div>
    );
}
export default App;
ReactDOM.render(<App />, document.getElementById('image-editor'));
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React Image Editor</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css"rel="stylesheet">
	<link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
        <div id='image-editor'>
            <div id='loader'>Loading....</div>
        </div>
</body>

</html>

Remove default save button and add custom button to save the image to server

Users 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.

Prevent default save option and save the image to specific location

Users 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 = true. Afterward, you can utilize getImageData to retrieve the current image data and convert it into a format like byte[], Blob, or base64 for further processing.

Events to handle save actions

The React 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, see the Events section on this page.

File opened event

The fileOpened event is triggered after an image is successfully loaded. It provides the OpenEventArgs object as the event argument, which includes:

  • fileName — The file name of the opened image.

  • fileType — The format/type of the opened image (PNG, JPEG, SVG, WEBP, or BMP).

Access these properties in your fileOpened handler to inspect the loaded image.

Saving event

The saving event is triggered when an image is being saved. It provides the SaveEventArgs object as the event argument, which includes:

  • fileName — The file name that will be used when saving the image.

  • fileType — The format/type the image will be saved in (PNG, JPEG, SVG, or WEBP).

  • cancel — Set to true to cancel the saving action.

Use these properties in the saving handler to inspect or cancel the save.

Created event

The created event is triggered once the React Image Editor component is created and initialized.

Destroyed event

The destroyed event is triggered once the React Image Editor component is destroyed or removed from the application.

See Also