欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-19 12:02:46
在我们使用qq上传头像,注册用户账号时是不是都会遇到上传图像,并根据自己的要求对图像进行裁剪,这是怎么实现的呐? 本文主要介绍了asp.net mvc实现上传头像加剪裁功...

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

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

运行效果截图如下:

实现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上传头像加剪裁功能的部分代码,小编分享给大家参考,希望对大家的学习有所帮助。