User Id :    Password :      New Member   Forgot Password  
 
How to crop images using JQuery in ASP.NET
Description By this article we came to know that how to put jQuery, JCrop and a FileUpload control together to allow users to upload images and crop them.   No. of Views     5683
  Rating     5
Author Gaurav   Posted On     15 Mar 2010
Tags Java Script,JQuery and ASP.Net    

Sample Code   Download Code

Upload and Crop Images in jQuery in ASP.NET

There's a fantastic library that really simplifies Javascript development, and is already attracting a goodly number of plug-ins.
Here's how to put jQuery, JCrop and a FileUpload control together to allow users to upload images and crop them.

If you haven't already done so, you will need to download the jQuery library. The current version, and therefore the one that is used in this sample is 1.2.6. Then you need to get hold of the files for JCrop. The download also includes a number of samples which show the features offered by JCrop, such as specifying a fixed aspect or minimum and maximum sizes etc, but this sample uses the default out-of-the-box settings. Be sure to get version 0.9.8 or later, as previous versions had trouble working with IE8 when compatability mode is switched off. Finally you could probably do with making sure that your VS 2008 or VWD 2008 has SP1 applied together with this hotfix which enables Intellisense on javascript files, including jQuery and JCrop. You need to get hold of the vsdoc.js file too.

This example consists of a single page. It caters for three actions: first, the user is presented with an Upload control to locate and upload their image; second, they will be presented with their uploaded image ready for cropping; and finally, they are presented with the cropped result. To accommodate this, three Panel controls are added to the page. The first, pnlUpload contains the FileUpload control and a button linked to an event handler for its click event. The second, pnlCrop contains an Image control and a button - again, linked to an event handler for its Click event. It also contains 4 HiddenField controls. They will be explained shortly. The Panel's Visibility is set to false. The final Panel, pnlCropped contains an Image control, and its Visibility is also set to false.











Some Javascript is required. If you were starting this kind of application from scratch, it would need an awful lot of javascript. However, jQuery's real power is illustrated by just how little javascript this page actually needs. To make use of both jQuery and JCrop, they need to be linked to in the head section of the page, along with the css file that comes as part of the JCrop download. The link to jQuery makes use of the copy available from the Google Ajax API Library for better caching and faster downloading:

 



All that's now needed to activate JCrop is a handful of lines of Javascript, which go within It really is that simple. Jcrop has been applied to the image that has the id of imgCrop (the one in pnlCrop), and an event handler has been added to the select event of the cropper. This will happen when the user has completed selecting the area of the image they want to keep. The handler makes use of the storeCoords function, which sets the values of the HiddenFields, passing them the x and y coordinates of the top left of the selection relative to the image, and the width and height of the selection. That's all ASP.NET needs to know in order to process the image on the server. Now on to the server-side code.
There are 4 namespaces that need to be referenced in additional to the default ones that a new Web Form brings in:
 
Since we will be working with instances of System.Drawing.Image as well as System.Web.UI.WebControls, which also has an Image class, I have aliased System.Drawing.Image, which will permit me to use a shorthand to reference the System.Drawing.Image classes. The first four lines of the code-behind simply set a variable to point to the file system path of the directory in which uploaded images will be stored, and show an empty Page_Load method:
 
String path = HttpContext.Current.Request.PhysicalApplicationPath + "images\\";
The next section covers the event handler for the Upload button click:

private void UploadImage
{
 Boolean FileOK = false;
Boolean FileSaved = false;
if (Upload.HasFile)
{ Session["WorkingImage"] = Upload.FileName; String FileExtension = Path.GetExtension(Session["WorkingImage"].ToString()).ToLower(); String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (FileExtension == allowedExtensions[i]) { FileOK = true; } } } if (FileOK) { try { Upload.PostedFile.SaveAs(path + Session["WorkingImage"]); FileSaved = true; } catch (Exception ex) { lblError.Text = "File could not be uploaded." + ex.Message.ToString(); lblError.Visible = true; FileSaved = false; } } else { lblError.Text = "Cannot accept files of this type."; lblError.Visible = true; } if (FileSaved) { pnlUpload.Visible = false; pnlCrop.Visible = true; imgCrop.ImageUrl = "images/" + Session["WorkingImage"].ToString(); } }
This code should be pretty familiar to anyone who has worked with the FileUpload control before. It simply checks to see if a file has been submitted, and ensures that it is of the right type. Then it saves the file to disk and the name of the file to a Session variable. Once the image has been saved, it is set as the ImageUrl of the Image control which has been targeted by JCrop and makes the containing Panel visible.
The next section covers the event handler for the Crop button:
 
   protected void btnCrop_Click(object sender, EventArgs e)
   { 
    string ImageName = Session["WorkingImage"].ToString();
    int w = Convert.ToInt32(W.Value);  
    int h = Convert.ToInt32(H.Value);
    int x = Convert.ToInt32(X.Value);
    int y = Convert.ToInt32(Y.Value);

    byte[] CropImage = Crop(path + ImageName, w, h, x, y);

    using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
    {
     ms.Write(CropImage, 0, CropImage.Length);
     using(SD.Image CroppedImage = SD.Image.FromStream(ms, true))
    {
      string SaveTo = path + "crop" + ImageName;
      CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
      pnlCrop.Visible = false;
      pnlCropped.Visible = true;
      imgCropped.ImageUrl = "images/crop" + ImageName;
    }
    }
  }
This section references the image name from the Session variable, and then declares a number of integers that get their values from the HiddenFields that JCrop wrote to when the user selected the area they wanted to keep.

It then calls a method called Crop() to perform the actual cropping of the image (more of which soon). Crop() returns a byte array, which is written to a MemoryStream so that it can be used and converted back to an Image. This is then prefixed with the word "crop" in its name before being saved to disk and displayed to the user.
The Crop() method is below:

 
   static byte[] Crop(string Img, int Width, int Height, int X, int Y)
   {
     try
     {
       using (SD.Image OriginalImage = SD.Image.FromFile(Img))
       {
         using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
         {
           bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
           using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
           {
             Graphic.SmoothingMode = SmoothingMode.AntiAlias;
             Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
             Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
             Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
             MemoryStream ms = new MemoryStream();
             bmp.Save(ms, OriginalImage.RawFormat);
             return ms.GetBuffer();
           }
         }
       }
     }

     catch (Exception Ex)
     {
       throw (Ex);
     }
   }
Enjoy JQuery Coding
Gaurav

About Author

About Author I am as simple as c#. Gaurav
No Photo
 
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Rkthiyaku on 08 Aug 2011 at 05:20 AM
 
hi... how to prevent autopostback during dropdownlist selectedindexchanged event..
 
 
   
Write your comment here.
Comment
Verification Code