Resize in the ASP.NET CORE Image Editor control

20 Feb 20254 minutes to read

The resize feature in an Image Editor is a valuable tool that empowers users to modify the size or dimensions of an image to meet their specific requirements. Whether it’s for printing, web display, or any other purpose, this feature allows users to tailor images to their desired specifications.

Apply resize to the image

The Image Editor control includes a resize method, which allows you to adjust the size of an image. This method takes three parameters that define how the resizing should be carried out:

  • width: Specifies the resizing width of the image.

  • height: Specifies the resizing height of the image.

  • isAspectRatio: Specifies a boolean value indicating whether the image should maintain its original aspect ratio during resizing.

    • When set to true, the image maintains its original aspect ratio. The width is applied as specified, and the height is automatically adjusted to maintain the aspect ratio.
    • When set to false, the image is resized according to the specified width and height, without maintaining the aspect ratio.
    • The default value is false.

Here is an example of resizing the image using the resize 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="aspectbtnClick" onclick="aspectClick()" cssClass="e-primary" content="Aspect Ratio"></ejs-button>
<ejs-button id="nonaspectbtnClick" onclick="nonaspectClick()" cssClass="e-primary" content="Non Aspect Ratio"></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 aspectClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.resize(400, 300, true);
    }
    function nonaspectClick() {
        var imgObj = ej.base.getComponent(document.getElementById('image-editor'), 'image-editor');
        imgObj.resize(400, 300, false);
    }

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

Resizing event

The resizing event is triggered when resizing the image. This event provides information encapsulated within an object, which includes details about the previous and current height and width of an image.

The parameter available in ResizeEventArgs is,

  • ResizeEventArgs.previousWidth - The width of the image before resizing is performed.

  • ResizeEventArgs.previousHeight - The height of the image before resizing is performed.

  • ResizeEventArgs.width - The width of the image after resizing is performed.

  • ResizeEventArgs.height - The width of the image after resizing is performed.

  • ResizeEventArgs.isAspectRatio - The type of resizing performed such as aspect ratio or non-aspect ratio.

  • ResizeEventArgs.cancel - Specifies a boolean value to cancel the resizing action.