Online image cropping on ASP. NET platform

Source: Internet
Author: User

Recently, there was an online image cropping function in the project. I used the following method to find information: jquery. jcrop is used for front-end display, and the system. Drawing. Image class is used for cropping in the background.

1. Foreground display implementation

Find this jquery. jcrop on the Internet. After reading it, we find that the effect it provides can fully meet the project requirements.

Official Website: http://deepliquid.com/content/jcrop.html. For more information, see.

The page first references related styles and scripts:

  <  Link  Href  = "Styles/jquery.jcrop.css"  REL  = "Stylesheet"  Type  = "Text/CSS"/> <  Script  SRC  = "Scripts/jquery-1.4.1.js" Type  = "Text/JavaScript"> </  Script  > <  Script  SRC  = "Scripts/jquery. jcrop. js"  Type  = "Text/JavaScript"> </  Script  > 

Page bodyCode:

  <  ASP  :  Label  ID = "Label1"  Text  = "Original Image"  Runat  = "Server"> </  ASP  :  Label  > <  BR  /> <  ASP  :  Image  ID  = "Target"  Runat = "Server"/> <  BR  /> <  ASP  :  Label  ID  = "Label2"  Runat  = "Server"  Text  = "Final display effect"> </  ASP  :  Label  > <  Div ID  = "Preimg"  Style  ="  Width  : 150px;  Height  : 80px;  Overflow  : Hidden; "> <  ASP  :  Image  ID  = "Preview"  ALT  = "Preview"  Runat = "Server"/> </  Div  > 

The width and height values of the style with the ID of preimg are the size of the cropped image, and the overflow: hidden of the Div should be defined. the key CSS attribute that allows you to see the image cropping Effect in a timely manner is it.

Next, let's talk about the basic usage of jquery. jcrop. js and the implementation of related JavaScript.

First, define some temporary variables to save relevant parameters.

VaRJcrop_api, boundx, boundy;

Then, bind the jcrop function to the DOM element of the image. You can see the meaning of the method attributes in English.

$ ('# Target'). Jcrop ({onchange: updatepreview, onselect: updatepreview, onrelease: clearcoords, aspectratio: 150/80, minsize: _ minarray, setselect: _ array },Function(){VaRBounds =This. Getbounds (); boundx = bounds [0]; boundy = bounds [1]; Jcrop_api =This;});

// This method is used to display the image cropping Effect in a timely manner.

                Function Updatepreview (c ){If (Parseint (C. W)> 0 ){ VaR RX = 150/C. W; VaR Ry = 80/C. h; VaR _ Width; VaR _ Height; If (Math. Round (RX * boundx)> $ targetimg. Width () {_ width = $ targetimg. Width ();} Else {_ Width = math. Round (RX * boundx );} If (Math. Round (RY * boundy)> $ targetimg. Height () {_ Height = $ targetimg. Height ();} Else {_ Height = math. Round (RY * boundy);} $ ( '# Preview' ).Css ({width: _ width + 'Px' , Height: _ height + 'Px' , Marginleft: '-' + Math. Round (RX * C. X) + 'Px' , Margintop: '-' + Math. Round (RY * C. Y) +'Px' }) ;}$ ( '# X1' ). Val (C. X); $ ( '# Y1' ). Val (C. Y); $ ( '# Iwidth' ). Val (C. W); $ ( '# Iheight' ). Val (C. H );};

Another part of front-end code:

 <  Form  ID  = "Form1"  Runat  = "Server"> <  ASP :  Hiddenfield  ID  = "Hdnnewimgpath"  Runat  = "Server"/> <  ASP  :  Hiddenfield  ID  = "X1"  Runat  = "Server"/> <  ASP  :  Hiddenfield  ID = "Y1"  Runat  = "Server"/> <  ASP  :  Hiddenfield  ID  = "Iwidth"  Runat  = "Server"/> <  ASP  :  Hiddenfield  ID  = "Iheight"  Runat  = "Server"/> < BR  /> <  ASP  :  Button  ID  = "Saveimg"  Runat  = "Server"  Text  = "Crop and save images"  Onclick  = "Saveimg"  Onclientclick  = "Return checkimg ()"/> </  Form  > 

 Implementation of background code:

First, reference the relevant namespace

Using system. drawing;
Using system. Drawing. imaging;
Using system. Drawing. design;

Save the button method, get the relevant parameters from the page, and then call the cropping method.

 Protected void Saveimg ( Object Sender, Eventargs E ){ If (Ispostback ){ String Tempurl = Path . Combine (Configaccess . Uploadimagepath, _ URL ); Int Startx = Int . Parse (x1.value ); Int Starty = Int . Parse (y1.value ); Int Width = Int . Parse (iwidth. value ); Int Height = Int . Parse (iheight. Value); imgreducecutout (startx, starty, width, height, tempurl, tempurl ); This . Target. Visible =False ; This . Label1.visible = False ; This . Saveimg. Enabled = False ;}}
 
 

Next is the most important cropping method:

  Public void  imgreducecutout ( int  startx,  int  starty,  int  int_width,  int  int_height,  string  input_imgurl,  string  out_imgurl) { // upload the size of a standard image   int  int_standard_width = 150;  int  int_standard_height = 80;  // In fact, more parameters can be passed during the image cropping process, such as narrowing down the original image and then cropping it. in this example,   after cropping, scale the cropped image to 150x80  
// Create an image object through a connectionSystem. Drawing. Image oldimage = system. Drawing. image. fromfile (input_imgurl); oldimage. Save (server. mappath ("Temp.jpg"));//Copy the source image. HoweverCrop the image on temp.jpg, and overwrite the cropped image.
 
Oldimage. Dispose ();// Make sure to release the temporary image, orSubsequent operations on this graph willError reported, cause conflict
Bitmap Bm = New  Bitmap (Server. mappath ( "Temp.jpg" ));// Processing JPG Quality Functions  Imagecodecinfo [] Codecs = Imagecodecinfo . Getimageencoders (); Imagecodecinfo ICI = Null ; Foreach ( Imagecodecinfo Codec In Codecs ){ If (Codec. mimetype = "Image/JPEG" ) {ICI = codec; Break ;}} Encoderparameters Ep = New  Encoderparameters (); EP. Param [0] = New  Encoderparameter ( Encoder . Quality ,( Long ) Level ); // Crop an image  Rectangle Clonerect = New  Rectangle (Startx, starty, int_width, int_height );Pixelformat Format = BM. pixelformat; Bitmap Clonebitmap = BM. Clone (clonerect, format ); If (Int_width> int_standard_width ){ // Zoom out the image System. Drawing. Image Cutimg = clonebitmap. getthumbnailimage (int_standard_width, int_standard_height, New System. Drawing. Image . Getthumbnailimageabort (Thumbnailcallback ), Intptr . Zero); cutimg. Save (out_imgurl, ici, EP); cutimg. Dispose ();} Else { // Save the image Clonebitmap. Save (out_imgurl, ici, EP);} clonebitmap. Dispose (); BM. Dispose ();} Public bool Thumbnailcallback (){ Return false ;}
 
Now, the online image cropping function is complete.

Main Page source code: Source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.