Filters in the ASP.NET CORE Image Editor control

18 Mar 20255 minutes to read

Filters are pre-defined effects that can be applied to an image to alter its appearance or mood. Image filters can be used to add visual interest or to enhance certain features of the image. Some common types of image filters include cold, warm, chrome, sepia, and invert. This can be done by either using the toolbar or the applyImageFilter method which takes a single parameter: the filter applied to an image.

Apply filter effect

The applyImageFilter method is utilized to apply filters to an image. By passing the desired filter type as the first parameter of the method, specified as ImageFilterOption the method applies the corresponding filter to the image. This allows for easy and convenient application of various filters to enhance or modify the image based on the chosen filter type.

The applyImageFilter method is used to perform filtering by specifying the type of filter as ImageFilterOption and send it a first parameter of the method.

Here is an example of filtering using the applyImageFilter method.

@{
    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="chromebtnClick" onclick="chromeClick()" cssClass="e-primary" content="Chrome"></ejs-button>
<ejs-button id="coldbtnClick" onclick="coldClick()" cssClass="e-primary" content="Cold"></ejs-button>
<ejs-button id="warmbtnClick" onclick="warmClick()" cssClass="e-primary" content="Warm"></ejs-button>
<ejs-button id="grayScalebtnClick" onclick="grayScaleClick()" cssClass="e-primary" content="GrayScale"></ejs-button>
<ejs-button id="sepiabtnClick" onclick="sepiaClick()" cssClass="e-primary" content="Sepia"></ejs-button>
<ejs-button id="invertbtnClick" onclick="invertClick()" cssClass="e-primary" content="Invert"></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 chromeClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.applyImageFilter(ej.imageeditor.ImageFilterOption.Chrome);
    }
    function coldClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.applyImageFilter(ej.imageeditor.ImageFilterOption.Cold);
    }
    function warmClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.applyImageFilter(ej.imageeditor.ImageFilterOption.Warm);
    }
    function grayScaleClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.applyImageFilter(ej.imageeditor.ImageFilterOption.Grayscale);
    }
    function sepiaClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.applyImageFilter(ej.imageeditor.ImageFilterOption.Sepia);
    }
    function invertClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.applyImageFilter(ej.imageeditor.ImageFilterOption.Invert);
    }

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

ImageEditor Sample

Image filtering event

The imageFiltering event is triggered when applying filtering on the image. This event is passed an object that contains information about the filtering event, such as the type of filtering.

The parameter available in the ImageFilterEventArgs event is,

ImageFilterEventArgs.filter - The type of filtering as ImageFilterOption to be applied in the image editor.

ImageFilterEventArgs.cancel – Specifies to cancel the filtering action.