How to Crop and Upload image using JQuery

In this blog, I will explain how to crop an image and then upload using JQuery. Below code will allow you to crop an image as soon as you will select an image to upload. This implementation has been done using JQuery.

How to Crop and Upload an Image using JQuery

Below code will show you how you can crop an image using JQuery and then upload to your desired folder.

HTML and JQuery Code to Crop an Image:

<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <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
            var type = $(this)[0].files['0'].type;
            document.getElementById('hdnFileType').value = type;


        $('#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) {
    <form id="form1" runat="server">
   <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="4" width="4"></canvas>
<br />
<input type="button" id="btnCrop" value="Crop" style="display: none" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" 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" />
    <asp:HiddenField ID="hdnFileType" runat="server" />

C# Code To Upload Cropped Image:

    protected void Upload(object sender, EventArgs e)
        string base64 = Request.Form["imgCropped"];
        byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);
        string fileType = hdnFileType.Value == "image/jpeg" ? ".jpg" : ".png";
        string name = DateTime.Now.ToFileTime().ToString();
        using (FileStream stream = new FileStream(Server.MapPath("~/Images/" + name + fileType), FileMode.Create))
            stream.Write(bytes, 0, bytes.Length);

Once you will select an image move your cursor over image to crop image, once area selected click on crop button and then click on upload to upload cropped image.
Output Screen:

image cropping in using jquery

image cropping in using jquery

Protected by Copyscape


  1. I have similar approach using mvc5


Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks


C# 6.0 New Features

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners