客户端js不能操作文件,所以只能先上传图片再在服务器端剪切。
1、上传图片
2、js剪切图片(其实只是选取要剪切的部分)
3、服务器端剪切
(1)在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。
其中剪切图片源码:
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
publicclassCut
{
///<summary>
///裁剪图片
///</summary>
///<paramname="sourceImg">原图片路径</param>
///<paramname="desImg">裁剪图片路径</param>
///<paramname="left">X</param>
///<paramname="top">Y</param>
///<paramname="width">宽</param>
///<paramname="height">高</param>
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();
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
publicclassCut
{
///<summary>
///裁剪图片
///</summary>
///<paramname="sourceImg">原图片路径</param>
///<paramname="desImg">裁剪图片路径</param>
///<paramname="left">X</param>
///<paramname="top">Y</param>
///<paramname="width">宽</param>
///<paramname="height">高</param>
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中剪切,可回传文件流。用参数传递坐标。
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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;
}
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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;
}
}
}