How To Crop Image And Save The Cropped Image Using ASP.NET And jQuery

Step 1

Create New Project

Go to File > New > Project > ASP.NET Web Application > Enter Application name > Click OK.

Step 2

Download CSS jquery.Jcrop.css and JS jquery.Jcrop.js from https://github.com/tapmodo/Jcrop also download jQuery.min.js from https://jquery.com/

Step 3

Create CSS and JS folder

Right click on the project name > create a folder like CSS and JS. Add those, which you downloaded from the above Websites.

Step 4

Add a Webpage and Design for Crop & Save

Right click on the project name from Solution Explorer > Add > New item > Select Web form> Enter page name > Add.

Step 5

Add the code given below in head section into index.aspx. 

  1. <link href="css/jquery.Jcrop.css" rel="stylesheet" />  
  2. <script src="js/jquery.min.js"></script>  
  3. <script src="js/jquery.Jcrop.js"></script>  
  4. <script type="text/javascript">  
  5.     $(document).ready(function() {  
  6.         $('#<%=imgUpload.ClientID%>').Jcrop({  
  7.             onSelect: SelectCropArea  
  8.         });  
  9.     });  
  10.   
  11.     function SelectCropArea(c) {  
  12.         $('#<%=X.ClientID%>').val(parseInt(c.x));  
  13.         $('#<%=Y.ClientID%>').val(parseInt(c.y));  
  14.         $('#<%=W.ClientID%>').val(parseInt(c.w));  
  15.         $('#<%=H.ClientID%>').val(parseInt(c.h));  
  16.     }  
  17. </script>   

Step 6

Similarly, add the code given below in body section into index.aspx. 

  1. <form id="form1" runat="server">  
  2.     <h3>Image Upload, Crop & Save using ASP.NET & Jquery</h3>  
  3.     <table>  
  4.         <tr>  
  5.             <td> Select Image File : </td>  
  6.             <td>  
  7.                 <asp:FileUpload ID="FU1" runat="server" /> </td>  
  8.             <td>  
  9.                 <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /> </td>  
  10.         </tr>  
  11.         <tr>  
  12.             <td colspan="3">  
  13.                 <asp:Label ID="lblMsg" runat="server" ForeColor="Red" /> </td>  
  14.         </tr>  
  15.     </table>  
  16.     <asp:Panel ID="panCrop" runat="server" Visible="false">  
  17.         <table width="50%">  
  18.             <tr>  
  19.                 <td>  
  20.                     <asp:Image ID="imgUpload" runat="server" Width="100%" /> </td>  
  21.             </tr>  
  22.             <tr>  
  23.                 <td>  
  24.                     <asp:Button ID="btnCrop" runat="server" Text="Crop & Save" OnClick="btnCrop_Click" /> </td>  
  25.             </tr>  
  26.             <tr>  
  27.                 <td>  
  28.                     <asp:HiddenField ID="X" runat="server" />  
  29.                     <asp:HiddenField ID="Y" runat="server" />  
  30.                     <asp:HiddenField ID="W" runat="server" />  
  31.                     <asp:HiddenField ID="H" runat="server" /> </td>  
  32.             </tr>  
  33.         </table>  
  34.     </asp:Panel>  
  35. </form>   

Step 7

Add a folder to save the uploaded images.

Right click on the project name from Solution Explorer > Add > New Folder > Rename the folder like UploadImages.

Step 8

Write the code given below in btnUpload_Click event to upload an image. 

  1. protected void btnUpload_Click(object sender, EventArgs e) {  
  2.     // Upload Original Image Here  
  3.     string uploadFileName = "";  
  4.     string uploadFilePath = "";  
  5.     if (FU1.HasFile) {  
  6.         string ext = Path.GetExtension(FU1.FileName).ToLower();  
  7.         if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png") {  
  8.             uploadFileName = Guid.NewGuid().ToString() + ext;  
  9.             uploadFilePath = Path.Combine(Server.MapPath("~/UploadImages"), uploadFileName);  
  10.             try {  
  11.                 FU1.SaveAs(uploadFilePath);  
  12.                 imgUpload.ImageUrl = "~/UploadImages/" + uploadFileName;  
  13.                 panCrop.Visible = true;  
  14.             } catch (Exception) {  
  15.                 lblMsg.Text = "Error! Please try again.";  
  16.             }  
  17.         } else {  
  18.             lblMsg.Text = "Selected file type not allowed!";  
  19.         }  
  20.     } else {  
  21.         lblMsg.Text = "Please select file first!";  
  22.     }  
  23. }   

Step 9

Write the code in btnCrop_Click event to crop & save cropped image.

  1. // Crop Image Here & Save  
  2. string fileName = Path.GetFileName(imgUpload.ImageUrl);  
  3. string filePath = Path.Combine(Server.MapPath("~/UploadImages"), fileName);  
  4. string cropFileName = "";  
  5. string cropFilePath = "";  
  6. if (File.Exists(filePath)) {  
  7.     System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);  
  8.     Rectangle CropArea = new Rectangle(Convert.ToInt32(X.Value), Convert.ToInt32(Y.Value), Convert.ToInt32(W.Value), Convert.ToInt32(H.Value));  
  9.     try {  
  10.         Bitmap bitMap = new Bitmap(CropArea.Width, CropArea.Height);  
  11.         using(Graphics g = Graphics.FromImage(bitMap)) {  
  12.             g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), CropArea, GraphicsUnit.Pixel);  
  13.         }  
  14.         cropFileName = "crop_" + fileName;  
  15.         cropFilePath = Path.Combine(Server.MapPath("~/UploadImages"), cropFileName);  
  16.         bitMap.Save(cropFilePath);  
  17.         Response.Redirect("~/UploadImages/" + cropFileName, false);  
  18.     } catch (Exception ex) {  
  19.         throw;  
  20.     }  
  21. }   

Step 10

Run the Application

Click Choose file, select an image and click Upload button, followed by the image, which will appear on the screen, as shown below.

Select the area, which you want to crop and save, as shown below,

 
After selecting the area, click Crop & Save button. The image will crop as much you selected and save into folder like UploadImages.
Ebook Download
View all
Learn
View all