---
title: "交友类网站裁剪照片源码"
author: "varsoft"
date: "2010-02-07T03:45:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2010-02-07T03:45:00Z"
id: 871536
url: "https://varsoft.iteye.com/blog/871536"
---

客户端js不能操作文件，所以只能先上传图片再在服务器端剪切。

1、上传图片

2、js剪切图片（其实只是选取要剪切的部分）

3、服务器端剪切

（1）在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。

其中剪切图片源码：

![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)Code  
&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;usingSystem;  
usingSystem.Collections.Generic;  
usingSystem.Text;  
usingSystem.Drawing;

publicclassCut  
{  
///&lt;summary&gt;  
///裁剪图片  
///&lt;/summary&gt;  
///&lt;paramname="sourceImg"&gt;原图片路径&lt;/param&gt;  
///&lt;paramname="desImg"&gt;裁剪图片路径&lt;/param&gt;  
///&lt;paramname="left"&gt;X&lt;/param&gt;  
///&lt;paramname="top"&gt;Y&lt;/param&gt;  
///&lt;paramname="width"&gt;宽&lt;/param&gt;  
///&lt;paramname="height"&gt;高&lt;/param&gt;  
publicstaticvoidCutImage(stringsourceImg,stringdesImg,intleft,inttop,intwidth,intheight)  
{  
System.Drawing.Imageimg=System.Drawing.Bitmap.FromFile(sourceImg);  
System.Drawing.ImageimgToSave=newSystem.Drawing.Bitmap(width,height);  
System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(imgToSave);  
RectangleFsourceRect=newRectangleF(left,top,width,height);  
RectangleFdestinationRect=newRectangleF(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）在ashx中剪切，可回传文件流。用参数传递坐标。

![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)Code  
&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt;http://www.CodeHighlighter.com/&lt;br /&gt;&lt;br /&gt;--&gt;  
usingSystem;  
usingSystem.Web;  
usingSystem.Drawing;  
usingSystem.IO;

publicclassImgCropper\_WebHandler:IHttpHandler  
{  
publicvoidProcessRequest(HttpContextcontext)  
{  
stringPic=Convert.ToString(context.Request\["p"]);  
intPointX=Convert.ToInt32(context.Request\["x"]);  
intPointY=Convert.ToInt32(context.Request\["y"]);  
intCutWidth=Convert.ToInt32(context.Request\["w"]);  
intCutHeight=Convert.ToInt32(context.Request\["h"]);  
intPicWidth=Convert.ToInt32(context.Request\["pw"]);  
intPicHeight=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);  
}

publicMemoryStreamResetImg(HttpContextcontext,stringImgFile,intPicWidth,intPicHeight,intPointX,intPointY,intCutWidth,intCutHeight)  
{  
ImageimgPhoto=Image.FromFile(ImgFile);  
BitmapbmPhoto=newBitmap(CutWidth,CutHeight,System.Drawing.Imaging.PixelFormat.Format24bppRgb);

GraphicsgbmPhoto=Graphics.FromImage(bmPhoto);  
gbmPhoto.DrawImage(imgPhoto,newRectangle(0,0,CutWidth,CutHeight),PointX\*imgPhoto.Width/PicWidth,PointY\*imgPhoto.Height/PicHeight,CutWidth\*imgPhoto.Width/PicWidth,CutHeight\*imgPhoto.Height/PicHeight,GraphicsUnit.Pixel);

//保存图片到服务器  
bmPhoto.Save(context.Server.MapPath("upload/")+Guid.NewGuid()+".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

//生成文件流回传  
MemoryStreamms2=newMemoryStream();  
bmPhoto.Save(ms2,System.Drawing.Imaging.ImageFormat.Jpeg);

imgPhoto.Dispose();  
gbmPhoto.Dispose();  
bmPhoto.Dispose();

returnms2;  
}

publicboolIsReusable  
{  
get  
{  
returnfalse;  
}  
}  
}