一、引入
拖放程序:Drag.js

图片切割:ImgCropper.js

缩放程序:Resize.js

二、前台文件

<body>之间插入</body>
<script type="text/javascript">
var isIE = (document.all) ? true : false;
var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
create: function () {
return function () { this.initialize.apply(this, arguments); }
}
}
var Extend = function (destination, source) {
for (var property in source) {
destination[property] = source[property];
}
}
var Bind = function (object, fun) {
return function () {
return fun.apply(object, arguments);
}
}
var BindAsEventListener = function (object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function (event) {
return fun.apply(object, [event || window.event].concat(args));
}
}
var CurrentStyle = function (element) {
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
function addEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
};
function removeEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.removeEventListener) {
oTarget.removeEventListener(sEventType, fnHandler, false);
} else if (oTarget.detachEvent) {
oTarget.detachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = null;
}
};
</script>
<script type="text/javascript" src="js/ImgCropper.js"></script>
<script type="text/javascript" src="js/Drag.js"></script>
<script type="text/javascript" src="js/Resize.js"></script>
<style type="text/css">
#rRightDown,#rLeftDown,#rLeftUp,#rRightUp,#rRight,#rLeft,#rUp,#rDown{ position:absolute; background:#FFF; border: 1px solid #333; width: 6px; height: 6px; z-index:500; font-size:0; opacity: 0.5; filter:alpha(opacity=50); }
#rLeftDown,#rRightUp{cursor:ne-resize;}
#rRightDown,#rLeftUp{cursor:nw-resize;}
#rRight,#rLeft{cursor:e-resize;}
#rUp,#rDown{cursor:n-resize;}
#rLeftDown{left:0px;bottom:0px;}
#rRightUp{right:0px;top:0px;}
#rRightDown{right:0px;bottom:0px;background-color:#00F;}
#rLeftUp{left:0px;top:0px;}
#rRight{right:0px;top:50%;margin-top:-4px;}
#rLeft{left:0px;top:50%;margin-top:-4px;}
#rUp{top:0px;left:50%;margin-left:-4px;}
#rDown{bottom:0px;left:50%;margin-left:-4px;}
#bgDiv{width:300px; height:400px; border:1px solid #666666; position:relative;}
#dragDiv{border:1px dashed #fff; width:100px; height:60px; top:50px; left:50px; cursor:move; }
</style>
<asp:FileUpload ID="fuimg" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" />
<br />
<asp:Image ID="img" runat="server" /> //上传图片的方法
<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="300"><div id="bgDiv">
<div id="dragDiv">
<div id="rRightDown"> </div>
<div id="rLeftDown"> </div>
<div id="rRightUp"> </div>
<div id="rLeftUp"> </div>
<div id="rRight"> </div>
<div id="rLeft"> </div>
<div id="rUp"> </div>
<div id="rDown"></div>
</div>
</div></td>
<td align="center"><div id="viewDiv" style="width:300px; height:300px;"> </div></td>
</tr>
</table>
<br />
<input name="" type="button" value="生成图片" onclick="Create()" />
<br /><br />
<img id="imgCreat" style="display:none;" />
<script>
<%=GetImg()%> //后台创建该方法 用于获取刚上传的图片名称
// var ic = new ImgCropper("bgDiv", "dragDiv", "1.jpg", {
// Width: 300, Height: 400, Color: "#000",
// Resize: true,
// Right: "rRight", Left: "rLeft", Up: "rUp", Down: "rDown",
// RightDown: "rRightDown", LeftDown: "rLeftDown", RightUp: "rRightUp", LeftUp: "rLeftUp",
// Preview: "viewDiv", viewWidth: 300, viewHeight: 300
// })
function Create() {
var p = ic.Url, o = ic.GetPos();
x = o.Left,
y = o.Top,
w = o.Width,
h = o.Height,
pw = ic._layBase.width,
ph = ic._layBase.height;
$("imgCreat").onload = function () { this.style.display = ""; }
$("imgCreat").src = "ImgCropper.ashx?p=" + p + "&x=" + x + "&y=" + y + "&w=" + w + "&h=" + h + "&pw=" + pw + "&ph=" + ph + "&" + Math.random(); //后台创建 ImgCropper.ashx 一般处理程序
}
</script>
后台代码

protected void btnUpload_Click(object sender, EventArgs e) //上传图片 { try { string proimg = Xiaobin.Utility.Tool.Upload(fuimg, new string[] { ".jpg", ".png", ".gif", ".bmp" }, 1, Server.MapPath("/upload/")); img.ImageUrl = "/upload/" + proimg; img.ToolTip = proimg; } catch (Exception ex) { Xiaobin.Utility.Tool.ExecJs(ex.Message, this.Page); } } public string GetImg() //获取图片名称 { string str = ""; string teImg = img.ImageUrl; str = "var ic = new ImgCropper('bgDiv', 'dragDiv', '" + teImg + "', {Width: 300, Height: 400, Color: '#000',Resize: true,Right: 'rRight', Left: 'rLeft', Up: 'rUp', Down: 'rDown',RightDown: 'rRightDown', LeftDown: 'rLeftDown', RightUp: 'rRightUp', LeftUp: 'rLeftUp',Preview: 'viewDiv', viewWidth: 300, viewHeight: 300})"; return str; }
三、创建 ImgCropper.ashx 一般处理程序


using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.IO; namespace XiaobinManage.Web { /// <summary> /// ImgCropper 的摘要说明 /// </summary> public class ImgCropper : 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(System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream); //ImgCropper.ashx?p=/upload/20150122/9f0e5c90-e748-4e13-8af5-d9fe2c7c154c.jpg&x=2&y=6&w=267&h=197&pw=300&ph=214&0.6161095989903618 //前台是显示成这种缓存图片 string[] ss = Pic.Split('/'); //分割 string small_img = System.Web.HttpContext.Current.Server.MapPath("/upload/" + ss[2] + "/small_" + ss[3]); Image imgPhoto = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(Pic)); // 把缓存图片生成出来 CutImage(System.Web.HttpContext.Current.Server.MapPath(Pic), small_img, PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight); } public MemoryStream ResetImg(string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight) { Image imgPhoto = Image.FromFile(ImgFile); Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics gbmPhoto = Graphics.FromImage(bmPhoto); 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); MemoryStream ms2 = new MemoryStream(); bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg); imgPhoto.Dispose(); gbmPhoto.Dispose(); bmPhoto.Dispose(); return ms2; } /// <summary> /// 裁剪图片 /// </summary> /// <param name="sourceImg">原图片路径</param> /// <param name="desImg">裁剪图片路径</param> /// <param name="left">X</param> /// <param name="top">Y</param> /// <param name="width">宽</param> /// <param name="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(); } public bool IsReusable { get { return false; } } } }