In this article I will explain how to Crop and Upload Image with Live Thumbnail Preview using jQuery and HTML5 Canvas.
The image will be cropped using the Jcrop jQuery plugin, and Live Thumbnail Preview is displayed using HTML5 Canvas.
Downloading the Jcrop Plugin
You can download the plugin using the following download link.
HTML Markup
The HTML Markup consists of an HTML FileUpload, HTML Image, HTML Button, HTML5 Canvas and some hidden fields.
<input type="file" id="FileUpload1" accept=".jpg,.png,.gif" />
<br />
<br />
<table border="0" cellpadding="0" cellspacing="5">
            <img id="Image1" src="" alt="" style="display: none" />
            <canvas id="canvas" height="5" width="5"></canvas>
<br />
<input type="button" id="btnCrop" value="Crop" style="display: none" />
<input type="hidden" name="imgX1" id="imgX1" />
<input type="hidden" name="imgY1" id="imgY1" />
<input type="hidden" name="imgWidth" id="imgWidth" />
<input type="hidden" name="imgHeight" id="imgHeight" />
<input type="hidden" name="imgCropped" id="imgCropped" />
Crop Image with Live Thumbnail Preview using jQuery and HTML5
Inside the jQuery document ready event handler, a change event handler is assigned to a FileUpload control and a click event handler is assigned to the HTML Button.
When a file is selected in the HTML FileUpload control, it is read using HTML FileReader API and then the image is displayed in the Image element and finally the Jcrop plugin is applied to the Image element.
Note: For more details on Image display using HTML5 File Reader API, please refer my article Preview multiple image files before upload when selected in FileUpload control using HTML5 and jQuery.
The Jcrop plugin makes a call to SetCoordinates function on its onChange and onSelect event handlers, which allow us to save the coordinates and dimensions i.e. height and width of the cropped image to the hidden fields.
When the Crop Button is clicked, it first creates an instance of the HTML5 Canvas and then loads the Image into an Image object with an OnLoad event handler.
Inside the OnLoad event handler, the original image is redrawn on the canvas using save the coordinates and dimensions i.e. height and width of the cropped image.
Finally the base64 string of the cropped image is fetched from the Canvas using the toDataURL method and is saved in the imgCropped hidden field which can be later sent to the server for saving the image.
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(function () {
    $('#FileUpload1').change(function () {
        var reader = new FileReader();
        reader.onload = function (e) {
                onChange: SetCoordinates,
                onSelect: SetCoordinates
    $('#btnCrop').click(function () {
        var x1 = $('#imgX1').val();
        var y1 = $('#imgY1').val();
        var width = $('#imgWidth').val();
        var height = $('#imgHeight').val();
        var canvas = $("#canvas")[0];
        var context = canvas.getContext('2d');
        var img = new Image();
        img.onload = function () {
            canvas.height = height;
            canvas.width = width;
            context.drawImage(img, x1, y1, width, height, 0, 0, width, height);
        img.src = $('#Image1').attr("src");
function SetCoordinates(c) {
Crop Image with Thumbnail Preview using jQuery and HTML5