为了使用户能自定义个人头像,需要提供一个对上传图片的截图功能,当前很多网站特别是SNS类网站都提供这样的功能,非常实用。本文主要是利用jQuery的imgAreaSelect插件实现。

首先引入三个文件:

 <script src="<%:Url.Content("~/UI/Scripts/jquery-1.8..min.js") %>"></script>
<link href='<%:Url.Content("~/UI//CSS/imgareaselect-default.css") %>' rel="stylesheet" />
<script src='<%:Url.Content("~/UI/Scripts/jquery.imgareaselect.pack.js")%>'></script>

前段主要代码:初始化所选择截取的图片

 $('#photo').imgAreaSelect({
aspectRatio: '1:1',
handles: true
, fadeSpeed: 200
, onSelectChange: preview
// , onSelectEnd: someFunction
});

设置所选区域大小值,与坐标:

function preview(img, selection) {
if (!selection.width || !selection.height)
return; var scaleX = 100 / selection.width;
var scaleY = 100 / selection.height; $('#left').val(selection.x1);
$('#top').val(selection.y1);
$('#w').val(selection.width);
$('#h').val(selection.height);
}

  实现代码:前台

            //上传图片
$("#File1").change(function () { $("#formSave").ajaxSubmit({
type: "POST",
url: "/Home/UpPic/",
dataType: "json",
success: function (data) {
if (data.msg == "OK") {
$("#photo").attr("src", data.path)
} else {
alert(data.msg);
}
}
});
}); //剪切后保存头像
$("#btnSaveImg").click(function () { if ($('#left').val() == "") {
alert("请先截取图片");
return;
} $("#formSave").ajaxSubmit({
type: "POST",
url: "/Home/SavePic/",
dataType: "json",
success: function (data) {
if (data.msg == "OK") {
$("#photo").attr("src", data.path)
alert("保存成功!");
} else {
alert(data.msg);
}
}
});
});

  实现代码后台:

 /// <summary>
/// 上传图片
/// </summary>
public void UpPic()
{
try
{
var file = Request.Files["File1"];
if (file.ContentLength == 0)
{
ReWrite("Error","请选择文件");
return;
}
if (file.ContentLength > 307200)
{
ReWrite("Error","文件过大");
return;
} int width = 0; int height = 0; using (Image originalImg = Image.FromFile(file.FileName))
{
double bi = originalImg.Width / originalImg.Height;
if (bi > 1.6)
{
width = 600;
height = (int)(600 / bi);
}
else
{
height = 360;
width = (int)(360 * bi);
}
} //w600 h360;
string extensionName = System.IO.Path.GetExtension(file.FileName).ToLower();
string fileName ="temp" + extensionName; string p = "/Images/" + fileName;
string path = Server.MapPath("~" + p);
// file.SaveAs(path);
Session["path"] = "~" + p;
CommonMethod.AutoMakeThumNail(file.FileName, path, width, height, PicThumNailModel.H);
ReWrite("OK", p);
}
catch (Exception ex)
{
ReWrite("Error",ex.Message);
return;
}
} public void SavePic()
{ string photo ="";
if (Session["path"] != null)
{
photo = Session["path"].ToString();
}
else
{
photo = "~/Images/20140430172226.png";
}
photo = Server.MapPath(photo);
using (Image originalImg = Image.FromFile(photo))
{ int imageWidth = originalImg.Width;
int imageHeight = originalImg.Height;
int cutTop = Int32.Parse(Request.Form["top"]);
int cutLeft = Int32.Parse(Request.Form["left"]);
int dropWidth = Int32.Parse(Request.Form["w"]);
int dropHeight = Int32.Parse(Request.Form["h"]);
ImageHelp imgHelp = new ImageHelp(); // string picPath = CommonMethod.GetConfig("HeadPicPath"); string extensionName = System.IO.Path.GetExtension(photo).ToLower();
string picName =DateTime.Now.ToString("yyyyMMddHHmmssff") + extensionName; string pp = "/Images/" + picName; imgHelp.GetPart(photo, Server.MapPath("/Images/"), 0, 0, dropWidth, dropHeight, cutLeft, cutTop, imageWidth, imageHeight, picName); ReWrite("OK", pp);
}
// DelPic(photo);
}

 

转载请注明出处:http://www.cnblogs.com/Xingsoft-555/

jquery 上传图片自由截取的更多相关文章

  1. jquery上传图片插件plupload

    官方网站:http://plupload.com/ jquery.plupload.queue插件,是上传图片组件很强大的插件.plupload 前端根据浏览器不同选择使用Html5. Gears, ...

  2. jquery 上传图片转为base64,ajax提交到后台

    支持多张图片上传.图片上传数量修改.可以删除 <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  3. jquery插件-自由拖拽

    最近工作不是很忙,学习之余想整理一些代码出来,首先想到的就是是js拖拽. 两年前去某公司面试的时候,曾经被问过这个问题,如何在页面上拖放元素,尽管现在看起来很简单,但当时的我半点思路都没有,面试想当然 ...

  4. jquery 上传图片即时预览功能

    <script type="text/javascript">        jQuery.fn.extend({            uploadPreview: ...

  5. jquery上传图片

    http://www.cnblogs.com/wutao/archive/2010/01/28/1658496.html http://www.cnblogs.com/cloudgamer/archi ...

  6. jQuery 超过字符截取部分用星号表示

    $(function(){ var str = $('#num').text(); if (str.length >15) { var strend = str.substring(4,str. ...

  7. ajax+jquery上传图片

    利用ajax进行图片上传,啥也不说了,上代码~ <input type="file" id="uploadImg"> <span  oncli ...

  8. javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法

    下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...

  9. jQuery常见面试题(转)

    代码以jQuery 1.83 为例 一 :Q: What is the difference between .get(), [], and .eq()? A: eq返回原生jQuery对象,截取某些 ...

随机推荐

  1. Codeforces 938.D Buy a Ticket

    D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. Nginx高级应用之Location Url 配置

    原文地址:https://www.linuxidc.com/Linux/2017-03/141910.htm 基本配置 为了探究nginx的url配置规则,当然需要安装nginx.我使用了vagran ...

  3. Sql2008 全文索引创建

    在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了.   下面简明的介绍如何使用Sql2008 全文索引 一.检查 ...

  4. maven创建spring项目之后,启动报错java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    出错情景:maven中已经加载了spring的核心包,但是项目启动时,报错: org.apache.catalina.core.StandardContext listenerStart严重: Err ...

  5. javaFX8初探(环境搭建)

    1:下载java8  Oracle官网2:下载eclipse4.4 eclipse官网3:安装e(fx)clipse插件 http://download.eclipse.org/efxclipse/u ...

  6. 提高效率!15款最好的 Bug 跟踪应用程序

    当涉及到开发项目时,其中比较重要的事情是它需要某​​种形式的错误和问题跟踪工具来发现和解决问题,否则会浪费大量的时间. 此外,你总是要标签应用来标示那些悬而未决的问题,而这种分期执行的项目进度将帮助您 ...

  7. 【BZOJ】3039: 玉蟾宫 悬线法

    [题意]给定01矩阵,求最大全1子矩阵.n,m<=1000. [算法]动态规划(悬线法) [题解]★对于01矩阵中的任意一个全1极大子矩阵,都可以在其上边界遇到的障碍点处悬线到下边界的点x,则点 ...

  8. JAVA 企业培训

  9. kali2.0安装虚拟机工具

    kali2.0无法安装虚拟机工具,显示VMware Tools无法用于该虚拟机,或者安装之后无法进行复制.粘贴等操作. 解决办法: step1: 更换源 root@starnight:~# vim / ...

  10. sql统计字符串出现次数技巧

    在牛客网上看到一道题,感觉挺有趣,是用sql统计字符串出现的次数. 这里提供一种思路,比如统计字符串A中子串B的出现次数: SELECT (LENGTH(A) - LENGTH(REPLACE(A, ...