Asp.net mvc实现上传头像加剪裁功能

所属分类: 网络编程 / ASP.NET 阅读数: 380
收藏 0 赞 0 分享

在我们使用QQ上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐?

本文主要介绍了Asp.net mvc实现上传头像加剪裁功能,分享给大家供大家参考。具体如下: 

运行效果截图如下:

具体代码如下:

前台代码

<link href="~/Content/fineuploader.css" rel="stylesheet" />
<link href="~/Content/jquery.Jcrop.min.css" rel="stylesheet" />
<link href="~/Content/crop.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.fineuploader-3.1.min.js"></script>
<script src="~/Scripts/jquery.Jcrop.min.js"></script>
<script src="~/Scripts/crop.js"></script>

<div id="jquery-wrapped-fine-uploader"></div>
 <div id="message"></div>
 <div id="crop_wrap">
  <div id="crop_holder">
   <div id="crop_area" class="border">
    <img id="crop_image" alt="" src="" class="preview-image" style="display: none" />
   </div>
   <div id="preview_area">
    <div id="preview_title">当前头像</div>
    <div id="preview_large_text" class="preview-text">180px × 180px</div>
    <div id="preview_large_wrap" class="border">
     <img id="preview_large" alt="" src="@ViewBag.Path" class="preview-image" style=""/></div>
   </div>
  </div>
  <div id="crop_operation" style="display: none;">
   <form id="form_crop" action="/home/index" method="post">
    <input type="hidden" name="x" id="x">
    <input type="hidden" name="y" id="y">
    <input type="hidden" name="w" id="w">
    <input type="hidden" name="h" id="h">
    <input type="hidden" name="imgsrc" id="imgsrc">
    <input id="crop_operation_submit" type="submit" value="裁切并保存" /><span id="crop_operation_msg" style="display: none" class="green"></span>
   </form>
  </div>
  <div id="croped_message" class="green"></div>
 </div>

后台代码

public ActionResult Index()
  {
   return View();
  }

  /// <summary>
  /// 保存缩略图
  /// </summary>
  /// <param name="form"></param>
  /// <returns></returns>
  [HttpPost]
  public ActionResult Index(FormCollection form)
  {
   int x = Convert.ToInt32(form["x"]);
   int y = Convert.ToInt32(form["y"]);
   int w = Convert.ToInt32(form["w"]);
   int h = Convert.ToInt32(form["h"]);
   string imgsrc = form["imgsrc"].Substring(0, form["imgsrc"].LastIndexOf("?"));
   string path = ImgHandler.CutAvatar(imgsrc, x, y, w, h);

   //保存Path
   
   ViewBag.Path = path;
   return View();
  }

  /// <summary>
  /// 上传头像
  /// </summary>
  /// <param name="qqfile"></param>
  /// <returns></returns>
  [HttpPost]
  public ActionResult ProcessUpload(string qqfile)
  {
   try
   {
    string uploadFolder = "/Upload/original/" + DateTime.Now.ToString("yyyyMM") + "/";
    string imgName = DateTime.Now.ToString("ddHHmmssff");
    string imgType = qqfile.Substring(qqfile.LastIndexOf("."));
    string uploadPath = "";
    uploadPath = Server.MapPath(uploadFolder);
    if (!Directory.Exists(uploadPath))
    {
     Directory.CreateDirectory(uploadPath);
    }
    using (var inputStream = Request.InputStream)
    {
     using (var flieStream = new FileStream(uploadPath + imgName + imgType, FileMode.Create))
     {
      inputStream.CopyTo(flieStream);
     }
    }

    return Json(new { success = true, message = uploadFolder + imgName + imgType });
   }
   catch (Exception e)
   {
    return Json(new { fail = true, message = e.Message });
   }
  }

以上就是实现Asp.net mvc上传头像加剪裁功能的部分代码,小编分享给大家参考,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

swfupload ajax无刷新上传图片实例代码

在这里上传图片就需要用到ajax无刷新上传图片,这里面包含的东西不是一点半点。这里用到的是一个插件swfupload实现无刷新上传图片,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

静态gb2312编码在项目传值出现中文乱码现象

参考的美工静态页面是gb2312格式的,当此编码拿到项目中后,utf-8编码的系统,加载页面时,会出现样式问题,比如不能正常居中等
收藏 0 赞 0 分享

System.Timers.Timer定时执行程序示例代码

如果是某个逻辑功能的定时,可以将code放到逻辑功能的类的静态构造函数中,在该逻辑类第一次执行时,静态构造函数会被调用,则定时自然启动
收藏 0 赞 0 分享

分享下Asp.Net面试题目及答案集合

这篇文章主要是总结asp.net开发人员在面试过程中常遇到的一些问题小结,需要的朋友可以参考下
收藏 0 赞 0 分享

给自定义Web控件添加事件(前后台代码)

给自定义控件(Web Control)添加事件具体前后台代码如下,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

ASP.NET过滤器的应用方法介绍

ASP.NET过滤器的应用方法介绍,需要的朋友可以参考一下
收藏 0 赞 0 分享

asp.net 图标提取以及图标转换的实例代码

asp.net 图标提取以及图标转换的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

页面爬虫(获取其他页面HTML)加载到自己页面示例

利用页面爬虫(获取其他页面HTML)加载到自己页面,实现所谓的小偷程序吧,具体实现代码如下,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

aspxgridview CustomButtonCallback 不支持弹出消息提示解决方法

aspxgridveiw是devexpress的一个grid控件,使用起来还不错,不能再 CustomButtonCallback 事件中使用response.write,具体的解决方法如下,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

关于中gridview 字符串截取的方法

在Gridview中,如果你的某一列字符串的长度过长,不做处理的话.那么将显示的奇丑无比,可以采取设置样式,将其显示为定长,可以在点击查看的时候,在另一个页面对其进行显示
收藏 0 赞 0 分享
查看更多