Source code of cut photos for friends websites

Source: Internet
Author: User

Client js cannot operate files, so you can only upload images and then cut images on the server.

1. upload images

2. js cut the image (in fact, just select the part to cut)

3. server side cut

(1) cut in the cs file on the page. You must put several hidden controls to return the coordinates selected by js.

Cut the source code of the image:

Code
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;

Public class Cut
{
/// <Summary>
/// Crop the image
/// </Summary>
/// <Param name = "sourceImg"> original image path </param>
/// <Param name = "desImg"> crop the image path </param>
/// <Param name = "left"> X </param>
/// <Param name = "top"> Y </param>
/// <Param name = "width"> width </param>
/// <Param name = "height"> height </param>
Public static void CutImage (string sourceImg, string desImg, int left, int top, int width, int height)
{
System. Drawing. Image img = System. Drawing. Bitmap. FromFile (sourceImg );
System. Drawing. Image imgToSave = new System. Drawing. Bitmap (width, height );
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (imgToSave );
RectangleF sourceRect = new RectangleF (left, top, width, height );
RectangleF destinationRect = new RectangleF (0, 0, width, height );

G. DrawImage (img,
DestinationRect,
SourceRect,
GraphicsUnit. Pixel
);
G. Save ();
ImgToSave. Save (desImg, System. Drawing. Imaging. ImageFormat. Jpeg );
G. Dispose ();
ImgToSave. Dispose ();
Img. Dispose ();
}

}

(2) cut in ashx to return the file stream. Use parameters to pass coordinates.

Code

Using System;
Using System. Web;
Using System. Drawing;
Using System. IO;

Public class ImgCropper_WebHandler: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
String Pic = Convert. ToString (context. Request ["p"]);
Int PointX = Convert. ToInt32 (context. Request ["x"]);
Int PointY = Convert. ToInt32 (context. Request ["y"]);
Int CutWidth = Convert. ToInt32 (context. Request ["w"]);
Int CutHeight = Convert. ToInt32 (context. Request ["h"]);
Int PicWidth = Convert. ToInt32 (context. Request ["pw"]);
Int PicHeight = Convert. ToInt32 (context. Request ["ph"]);

Context. Response. ContentType = "image/jpeg ";
ResetImg (context, System. web. httpContext. current. server. mapPath (Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight ). writeTo (context. response. outputStream );
}

Public MemoryStream ResetImg (HttpContext context, string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)
{
Image imgPhoto = Image. FromFile (ImgFile );
Bitmap BMP hoto = new Bitmap (CutWidth, CutHeight, System. Drawing. Imaging. PixelFormat. Format24bppRgb );

Graphics gbmPhoto = Graphics. FromImage (BMP hoto );
GbmPhoto. drawImage (imgPhoto, new Rectangle (0, 0, CutWidth, CutHeight), PointX * imgPhoto. width/PicWidth, PointY * imgPhoto. height/PicHeight, CutWidth * imgPhoto. width/PicWidth, CutHeight * imgPhoto. height/PicHeight, GraphicsUnit. pixel );

// Save the image to the server
BMP hoto. Save (context. Server. MapPath ("upload/") + Guid. NewGuid () + ". jpg", System. Drawing. Imaging. ImageFormat. Jpeg );

// Generate file stream return
MemoryStream ms2 = new MemoryStream ();
BMP hoto. Save (ms2, System. Drawing. Imaging. ImageFormat. Jpeg );

ImgPhoto. Dispose ();
GbmPhoto. Dispose ();
BMP hoto. Dispose ();

Return ms2;
}

Public bool IsReusable
{
Get
{
Return false;
}
}
}

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.