当初说这个需求的时候,在网上找了一点资料,但是基本上感觉不符合项目中的需求。参照一些项目,和同事的改造,终于是像点样子了。
截图大致截为3个像素,每个像素使用的地方也不同,考虑图片不会是很多,分别压缩保存下来。
首先需要下载Jcrop.js与uploadify.js 上传图片的插件和截图的插件
Comm_UploadJcrop.js
$("#preview_large2").css({
width: Math.round(rx boundx) + 'px',height: Math.round(ry boundy) + 'px',marginLeft: '-' + Math.round(rx c.x) + 'px',marginTop: '-' + Math.round(ry c.y) + 'px'
});
$pcnt = $('#preview_large3').parent(),xsize2 = $pcnt.height();
ysize2 = $pcnt.height();
var rx2 = xsize2 / c.w;
var ry2 = ysize2 / c.h;
$("#preview_large3").css({
width: Math.round(rx2 boundx) + 'px',height: Math.round(ry2 boundy) + 'px',marginLeft: '-' + Math.round(rx2 c.x) + 'px',marginTop: '-' + Math.round(ry2 c.y) + 'px'
});
}
};
region 上传图片
//上传头像
[HttpPost]
public JsonResult UploadifyHeader(HttpPostedFileBase fileData)
{
if (fileData != null)
{
try
{
// 文件上传后的保存路径
string fileName = Path.GetFileName(fileData.FileName);// 原始<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="/tag/mingcheng/" target="_blank" class="keywords">名称</a>
string fileExtension = Path.GetExtension(fileName); // <a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>扩展名
string saveName =DateTime.Now.ToString("yyyyMMddHHmmssffff")+"_Y" + fileExtension; // 保存<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="/tag/mingcheng/" target="_blank" class="keywords">名称</a>
string Url = "/Upload/" + CurrentUserInfo.Sys_RentCompany.CompanyKey + "/" + CurrentUserInfo.Comm_User.Id + "/UploadImg"; //在项目中创建一个Upload<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>夹存放<a href="/tag/shangchuan/" target="_blank" class="keywords">上传</a>和<a href="/tag/jiequ/" target="_blank" class="keywords">截取</a>的<a href="/tag/tupian/" target="_blank" class="keywords">图片</a> CurrentUserInfo.Sys_RentCompany.CompanyKey 为session 保存的值 这里对应的<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>夹的路径 自己可以定义
string ShowUrl = Url + "/" + saveName;
string filePhysicalPath = Server.MapPath(ShowUrl);
//当前登陆人<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>夹
CheckFileExistsCreateNew(Url);
fileData.SaveAs(filePhysicalPath);
//压缩<a href="/tag/tupian/" target="_blank" class="keywords">图片</a>
string YsFileImg = ImgHandler.YsImg(ShowUrl,filePhysicalPath);
return Json(YsFileImg);
}
catch (Exception ex)
{
return Json(new { Success = false,Message = ex.Message },JsonRequestBehavior.AllowGet);
}
}
else
{
return Json(new { Success = false,Message = "请选择要上传的文件!" },JsonRequestBehavior.AllowGet);
}
}
//上传头像
[HttpPost]
public JsonResult UploadifyImg(HttpPostedFileBase fileData)
{
if (fileData != null)
{
try
{
// 文件上传后的保存路径
string fileName = Path.GetFileName(fileData.FileName);// 原始<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="/tag/mingcheng/" target="_blank" class="keywords">名称</a>
string fileExtension = Path.GetExtension(fileName); // <a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>扩展名
string saveName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + "_Y" + fileExtension; // 保存<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="/tag/mingcheng/" target="_blank" class="keywords">名称</a>
//string filed = "/Upload/" + saveName;//路经
string Url = "/Upload/" + CurrentUserInfo.Sys_RentCompany.CompanyKey + "/" + CurrentUserInfo.Comm_User.Id + "/UploadImg";
string ShowUrl = Url + "/" + saveName;
string filePhysicalPath = Server.MapPath(ShowUrl);
//当前登陆人<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>夹
CheckFileExistsCreateNew(Url);
fileData.SaveAs(filePhysicalPath);
return Json(ShowUrl);
}
catch (Exception ex)
{
return Json(new { Success = false,JsonRequestBehavior.AllowGet);
}
}
//保存剪切的图片
[HttpPost]
public JsonResult JcropImg(FormCollection colls)
{
JsonFormatResult result = new JsonFormatResult { IsSuccess = true,Message = "保存成功!" };
try
{
int x = int.Parse(colls["x"]);
int y = int.Parse(colls["y"]);
int w = int.Parse(colls["w"]);
int h = int.Parse(colls["h"]);
string imgsrc = colls["imgsrc"];
string Path = ImgHandler.CutAvatar(imgsrc,x,y,w,h);
result.Data = Path;
}
catch (Exception e)
{
result.IsSuccess = false;
result.Message = e.Message;
}
return Json(result);
}