为了使用户能自定义个人头像,需要提供一个对上传图片的截图功能,当前很多网站特别是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. Listener 介绍

    当 web 应用在 web 容器中运行时,web 应用内部会不断地发生各种事件:如 web 应用启动.web 应用停止,用户 session 开始.用户 session 结束.用户请求到达等. 实际上 ...

  2. [ldap]ldap server安装以及图形化操作

    https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-a-basic-ldap-server-on ...

  3. 12.UiAutomator 获取系统信息

    一.Build构建信息 1.build类: Build类提供了硬件厂商.编号.序列号.SDK版本等重要信息. 类名:android.os.Build 常量名 说明 BOARD 底层板名称 BOOTLO ...

  4. OpenCV---分水岭算法

    推文: OpenCV学习(7) 分水岭算法(1)(原理简介简单明了) OpenCV-Python教程:31.分水岭算法对图像进行分割(步骤讲解不错) 使用分水岭算法进行图像分割 (一)获取灰度图像,二 ...

  5. OpenCV---对象测量

    一:获取图像的外接矩形boundingRect和几何距moments import cv2 as cv import numpy as np def measure_object(image): gr ...

  6. Centos 修改limits.conf open files后不生效的解决办法

    线上几台APACHE服务器报过三.四次open files的问题,导致服务不可用,执行ulimit -n 查看后,发现是默认的1024,找到原因所在了,就去修改下/etc/security/limit ...

  7. mysql数据库使用sql命令窗口查询的数据,改成sql语句导入到mysql数据库中

    1.查询语句为select * from t_table;导出的数据格式如下: 2.将数据文本备份,然后使用NOTEPAD++打开,然后只拷贝数据到新建txt中,然后进行如下替换: 1)将“ | ”分 ...

  8. poi复杂excel的实现

    一:前言 最近帮一个朋友做excel的导出功能,对于我来说还是挺头疼,我看了下表格样式,对于我来说还是挺头疼的,想当年耗子刚刚出社会的时候做的第一份工作,第一份任务就是把把word转换为html,在这 ...

  9. IDEA不显示更新、提交按钮

    问题描述: IDEA右上角不显示版本管理的“更新”.“提交”等按钮,左侧代码树中,也没有文件的状态 解决办法: 点击VCS-->Enable Version Control Integratio ...

  10. 前端开发进阶:推荐的 CSS 书写规范

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...