Asp.Net无刷新上传并裁剪头像
2011-11-29 16:24
337 查看
开发网站几乎都做过上传图片并截图,做个无刷新Asp.Net上传并截图示例
实现功能:
1.选择文件,自动上传并生成缩放图(上传带进度条),形成预览图
2.在预览区,实现鼠标拖拽截图区,截取图片(示例截图区按缩放图小边为截图正方形长度,可扩展为截图区可变形式)
3.点击保存,截取小图,保存截取图并显示在页面上,并删除原缩略图
示例截图:
View Code
这种就可以达到我文章开头的要求了
如果UploadAvatarHandler.ashx中要用到Session那就需求些额外的配置
在Global.asax中Application_BeginRequest加入下列代码
页面中加入隐藏域
<asp:HiddenField ID="hdSessionId" runat="server" />
并在页面的Load事件中,把SessionID赋给隐藏域
Uploadify()方法scriptData属性在做修改
以上操作,用于修改身份验证也一样。。。
CutPic.js中有一些可以扩展
比如:有人要求截图区域自己要可以拖动
把CutPic.js图片显示,截图区HTML变成这样
再给这些新添DIV写些样式。。。^_^! 这里自己扩展吧
显示区的JS加上最后一个参数
可能还有需求上传不要进度条,这个我没找到属性,有用过的可以指点一下,
不过这个问题没有属性也不是不可以解决的,可以在uploadify的JS文件中删除或注释掉append的这一段进度条HTML代码。
这样就做完了,Uploadify和这个CutPic.js几乎能做到所有现在能看到的上传截图功能,还有的自己扩展一下^-^!
PS:示例代码没有优化,请各位自己做做优化^_^!
Demo下载:http://files.cnblogs.com/zhongweiv/CutAvatar.rar
实现功能:
1.选择文件,自动上传并生成缩放图(上传带进度条),形成预览图
2.在预览区,实现鼠标拖拽截图区,截取图片(示例截图区按缩放图小边为截图正方形长度,可扩展为截图区可变形式)
3.点击保存,截取小图,保存截取图并显示在页面上,并删除原缩略图
示例截图:
View Code
<%@ WebHandler Language="C#" Class="CutAvatarHandler" %> using System; using System.Web; using System.Web.SessionState; public class CutAvatarHandler : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; System.Drawing.Bitmap bitmap = null; //按截图区域生成Bitmap System.Drawing.Image thumbImg = null; //被截图 System.Drawing.Graphics gps = null; //存绘图对象 System.Drawing.Image finalImg = null; //最终图片 if (HttpContext.Current.Session["UploadSession"] != null) { try { string pointX = context.Request.Params["pointX"]; //X坐标 string pointY = context.Request.Params["pointY"]; //Y坐标 string imgUrl = context.Request.Params["imgUrl"]; //被截图图片地址 string rlSize = context.Request.Params["maxVal"]; //截图矩形的大小 int finalWidth = 100; int finalHeight = 100; if (!string.IsNullOrEmpty(pointX) && !string.IsNullOrEmpty(pointY) && !string.IsNullOrEmpty(imgUrl)) { string ext = System.IO.Path.GetExtension(imgUrl).ToLower(); //上传文件的后缀(小写) bitmap = new System.Drawing.Bitmap(Convert.ToInt32(rlSize), Convert.ToInt32(rlSize)); thumbImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(imgUrl)); System.Drawing.Rectangle rl = new System.Drawing.Rectangle(Convert.ToInt32(pointX), Convert.ToInt32(pointY), Convert.ToInt32(rlSize), Convert.ToInt32(rlSize)); //得到截图矩形 gps = System.Drawing.Graphics.FromImage(bitmap); //读到绘图对象 gps.DrawImage(thumbImg, 0, 0, rl, System.Drawing.GraphicsUnit.Pixel); finalImg = PubClass.GetThumbNailImage(bitmap, finalWidth, finalHeight); string finalPath = "/User/final" + DateTime.Now.ToFileTime() + ext; finalImg.Save(HttpContext.Current.Server.MapPath(finalPath)); bitmap.Dispose(); thumbImg.Dispose(); gps.Dispose(); finalImg.Dispose(); GC.Collect(); PubClass.FileDel(HttpContext.Current.Server.MapPath(imgUrl)); context.Response.Write(finalPath); } } catch (Exception) { throw; } } else { context.Response.Write("erorr"); } } public bool IsReusable { get { return false; } } }
这种就可以达到我文章开头的要求了
[b]第三步:修复文件开头提到Uploadify用Session在Chrome和FireFox下的Bug (身份验证也一样有这个BUG,修复同理)[/b] |
在Global.asax中Application_BeginRequest加入下列代码
protected void Application_BeginRequest(object sender, EventArgs e) { //为了Uploadify在谷歌和火狐下不能上传的BUG try { string session_param_name = "ASPSESSID"; string session_cookie_name = "ASP.NET_SessionId"; if (HttpContext.Current.Request.Form[session_param_name] != null) { UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); } else if (HttpContext.Current.Request.QueryString[session_param_name] != null) { UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); } } catch { } //此处是身份验证 try { string auth_param_name = "AUTHID"; string auth_cookie_name = FormsAuthentication.FormsCookieName; if (HttpContext.Current.Request.Form[auth_param_name] != null) { UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); } else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) { UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); } } catch { } }
页面中加入隐藏域
<asp:HiddenField ID="hdSessionId" runat="server" />
并在页面的Load事件中,把SessionID赋给隐藏域
Uploadify()方法scriptData属性在做修改
$('#uploadify').uploadify({ //.... 'scriptData': { 'ASPSESSID': $('[id$=hdSessionId]').val() }, //..... });
以上操作,用于修改身份验证也一样。。。
[b]第四步:一些扩展 [/b] |
比如:有人要求截图区域自己要可以拖动
把CutPic.js图片显示,截图区HTML变成这样
<div id="bgDiv"> <div id="dragDiv"> <div id="rRightDown" style="right: 0; bottom: 0;"> </div> <div id="rLeftDown" style="left: 0; bottom: 0;"> </div> <div id="rRightUp" style="right: 0; top: 0;"> </div> <div id="rLeftUp" style="left: 0; top: 0;"> </div> <div id="rRight" style="right: 0; top: 50%;"> </div> <div id="rLeft" style="left: 0; top: 50%;"> </div> <div id="rUp" style="top: 0; left: 50%;"> </div> <div id="rDown" style="bottom: 0; left: 50%;"> </div> </div> </div>
再给这些新添DIV写些样式。。。^_^! 这里自己扩展吧
显示区的JS加上最后一个参数
function ShowImg(imagePath,imgWidth,imgHeight) { var imgPath = imagePath != "" ? imagePath : "!images/ef_pic.jpg"; var ic = new ImgCropper("bgDiv", "dragDiv", imgPath, imgWidth, imgHeight, { Right: "rRight", Left: "rLeft", Up: "rUp", Down: "rDown", RightDown: "rRightDown", LeftDown: "rLeftDown", RightUp: "rRightUp", LeftUp: "rLeftUp"}); }
可能还有需求上传不要进度条,这个我没找到属性,有用过的可以指点一下,
不过这个问题没有属性也不是不可以解决的,可以在uploadify的JS文件中删除或注释掉append的这一段进度条HTML代码。
这样就做完了,Uploadify和这个CutPic.js几乎能做到所有现在能看到的上传截图功能,还有的自己扩展一下^-^!
PS:示例代码没有优化,请各位自己做做优化^_^!
Demo下载:http://files.cnblogs.com/zhongweiv/CutAvatar.rar
相关文章推荐
- Asp.Net无刷新上传并裁剪头像
- Asp.Net无刷新上传并裁剪头像
- Asp.Net无刷新上传并裁剪头像
- Asp.Net无刷新上传并裁剪头像
- ASP.NET简单实现APP中用户个人头像上传和裁剪
- asp.net 头像上传裁剪模块
- asp.net 头像上传裁剪模块
- ASP.NET MVC 一款可预览、裁剪头像上传组件
- flash 头像上传裁剪预览支持多种语言php,jsp,asp,asp.net
- ASP.NET简单实现APP中用户个人头像上传和裁剪
- 无刷新附件上传 (asp.net版 )
- ASP.NET MVC在服务端把异步上传的图片裁剪成不同尺寸分别保存,并设置上传目录的尺寸限制
- Asp.net 2.0 无刷新图片上传+回显
- asp.net 文件上传与刷新与asp.net页面与iframe之间的数据传输
- [Flash FileUpload]用flash.net.FileReference实现ASP.NET无刷新文件上传
- 实现Asp.net mvc上传头像加剪裁功能
- 无刷新上传图片(asp.net mvc)
- asp.net+js 实现无刷新上传解析csv文件的代码
- Asp.net使用ajax无刷新上传文件(附源码)
- Jquery+ASP.NET 实现开心网上传头像剪裁功能