最近有这样一个需求,因为一个门户网站首页展示图片很长但很矮,自己截图怕有不到位,所以利用JQUERY 的 Jcrop组件做了一个在线裁图的功能。

初始化

        $('#oldpicImg').Jcrop({
onChange: showCoords,
onSelect: showCoords,
aspectRatio: null
});

因为在下面加了个选项,在意在change这个事件里面绑定重新初始化

        radios.change(function () {
imgParameter.scaling = this.value;
$('#oldpicImg').Jcrop({
onChange: showCoords,
onSelect: showCoords,
aspectRatio: imgParameter.scaling
});
})

在他本身的onChange和onSelect里,主要是存一下他的坐标,宽,高

function showCoords(obj) {
imgParameter.x =obj.x;
imgParameter.y = obj.y;
imgParameter.w =obj.w;
imgParameter.h =obj.h;
}

因为我们组件的原因,在IE7下有问题,会把图片弄为宽高为零,所以我又把他的宽高重新设置了一下

    function resetImgInfo(jqObj, width, height) {
jqObj.width(width).height(height).show();
}

还有就是图片太大了我做了一下自动等比缩放的功能, 在他加载完成的时候执行

 $cuttingPic.$oldpicImg.load(AutoResizeImage(0, 400, $cuttingPic.$oldpicImg[0]));
function AutoResizeImage(maxWidth, maxHeight, objImg) {
var img = new Image();
img.src = objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth == 0 && maxHeight == 0) {
Ratio = 1;
} else if (maxWidth == 0) {//
if (hRatio < 1) Ratio = hRatio;
} else if (maxHeight == 0) {
if (wRatio < 1) Ratio = wRatio;
} else if (wRatio < 1 || hRatio < 1) {
Ratio = (wRatio <= hRatio ? wRatio : hRatio);
}
if (Ratio < 1) {
w = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}

然后就用把值传向后台,我用的基于AJAX的WCF,

function cutImg() {
if (imgParameter.w != 0 && imgParameter.h != 0) {
SystemUtilService.CuttingImg(pageData.cuttingPicPath, imgParameter.x, imgParameter.y, imgParameter.w, imgParameter.h, imgParameter.curW, imgParameter.curH, function (data) { });
}
else {
//alert(2);
}
return true;
}

在后台这样来接受他

 [OperationContract]
public bool CuttingImg(string path, int x, int y, int w, int h, int picw, int pich)
{
string _path = AppDomain.CurrentDomain.BaseDirectory + path;
ImageHelper.CropImage(_path, w, h, x, y, picw, pich);
return true; }

这里给出一个工具类

 public class ImageHelper
{
public static void CropImage(string originamImgPath, int width, int height, int x, int y, int showWidth, int showHeight)
{
byte[] CropImage = Crop(originamImgPath, width, height, x, y, showWidth, showHeight);
using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
{
ms.Write(CropImage, 0, CropImage.Length);
using (System.Drawing.Image CroppedImage = System.Drawing.Image.FromStream(ms, true))
{ CroppedImage.Save(originamImgPath, CroppedImage.RawFormat);
}
}
}
private static byte[] Crop(string Img, int Width, int Height, int X, int Y, int showWidth, int showHeight)
{
try
{
using (Image OriginalImage = Image.FromFile(Img))
{ int picW = OriginalImage.Size.Width;
int picH = OriginalImage.Size.Height;
//int picW= 1920;
//int picH = 1080; int picWidth = Width * picW / showWidth;
int picHeight = Height * picH / showHeight;
int picX = X * picW / showWidth;
int picY = Y * picH / showHeight; using (Bitmap bmp = new Bitmap(picWidth, picHeight, OriginalImage.PixelFormat))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, picWidth, picHeight), picX, picY, picWidth, picHeight, GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw (Ex);
}
}
}

解释一下这里

int picWidth = Width * picW / showWidth; int picHeight = Height * picH / showHeight; int picX = X * picW / showWidth; int picY = Y * picH / showHeight;

因为在前台是等比压缩了,所以他的x,y,w,h与真实的是不一样的,所以在后台还有处理一下,前台记得传进去当前图片的高与宽

        $cuttingPic.$oldpicImg.attr("src", pageData.cuttingPicPath).load(function () {
AutoResizeImage(0, 400, $("#oldpicImg")[0]);
imgParameter.curW = $(this).width();
imgParameter.curH = $(this).height();
resetImgInfo($(this), imgParameter.curW, imgParameter.curH); });

.NET + Jcrop 实现在线裁图功能的更多相关文章

  1. 注意android裁图的Intent action

    现在很多开发者在裁图的时候还是使用com.android.camera.action.CROP 来调用 startActivity(). 这不是个好主意. 任何不是依android开头的Action ...

  2. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  3. TogetherJS – 酷!在网站中添加在线实时协作功能

    TogetherJS是一个免费.开源的 JavaScript 库,来自 Mozilla 实验室,可以实现基于 Web 的在线协作功能.把 TogetherJS 添加到您的网站中,您的用户可以在实时的互 ...

  4. protel DXP的类矢量图功能

    一.概述 在写论文的过程中,我们经常需要将protel DXP上的原理图贴入到WORD中.我们可以选择使用截图工具,然后再导入到WORD中.但是由于普通截图图形文件都是位图文件,当我们将图形文件导入W ...

  5. 【JEECG技术文档】JEECG在线聊天插件功能集成文档

    原文地址:http://jeecg.iteye.com/blog/2320670 JEECG在线聊天插件功能集成文档 前提: 采用jeecg_3.6.3版本以上(Maven工程) 插件项目: 在线聊天 ...

  6. 【Android】3.23 示例23--瓦片图功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 地图SDK自v3.6.0起,新增瓦片图层(tileOverlay), 该图层支持开发者添加自有瓦片数 ...

  7. vue 整合雪碧图功能

    1.通过命令新建一个vue项目 环境要求: 安装有 Node.js. vue. vue-cli . 创建项目: vue init webpack tx_demo cd tx_demo 进入项目,下载依 ...

  8. Java实现在线预览功能

    java实现在线预览功能,需要用到  jacob.dll jacob.jar   预览pdf所需js  pdfobject.min.js 将上传文件转为pdf保存. <div class=&qu ...

  9. 微信小程序一键生成源码 在线制作定制功能强大的微信小程序

    微信小程序发展到现在,短短的一年不到的时间(很快就要迎来微信小程序周年庆),在快迎来周年庆之际,百牛信息技术bainiu.ltd特记录一下这个发展的历程,用于将来见证小程序发展的辉煌时刻,我们还能知道 ...

随机推荐

  1. [LeetCode]题解(python):146-LRU Cache

    题目来源: https://leetcode.com/problems/lru-cache/ 实现一个LRU缓存.直接上代码. 代码(python): class LRUCache(object): ...

  2. 第一个只出现一次的字符,josephus环,最大子数组和

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXINT 0x7fffffff ...

  3. delphi程序设计之底层原理

    虽然用delphi也有7,8年了,但大部分时间还是用在系统的架构上,对delphi底层还是一知半解,今天在网上看到一篇文章写得很好,虽然是07年的,但仍有借鉴的价值. 现摘录如下: Delphi程序设 ...

  4. Silverlight第三方控件专题

    原文http://www.cnblogs.com/nasa/archive/2008/12/01/1344927.html 这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏 ...

  5. rsyslog ~ 波浪号

    <pre name="code" class="html">Using negation can be useful if you would li ...

  6. SPOJ 3267 求区间不同数的个数

    题意:给定一个数列,每次查询一个区间不同数的个数. 做法:离线+BIT维护.将查询按右端点排序.从左到右扫,如果该数之前出现过,则将之前出现过的位置相应删除:当前位置则添加1.这样做就保证每次扫描到的 ...

  7. Android中进行流量统计

    // ---------------------流量统计-------------------------------- try { PackageManager pm = getPackageMan ...

  8. AndroidContentProvider ContentResolver和ContentObserver的使用

    1.ContentProvider.ContentResolver和ContentObserver ContentProvider是Android的四大组件之一,可见它在Android中 的作用非同小 ...

  9. 一键搞定Java桌面应用安装部署 —— exe4j + Inno Setup 带着JRE, 8M起飞

    转载自:http://www.blogjava.net/huliqing/archive/2008/04/18/193907.html 对于作Java桌面应用来说,比较烦人的就是安装部署问题,客户端是 ...

  10. android入门——Activity(2)

    主要内容:一.IntentFlag  二.简单复杂数据传递  三.数据回传  四.打开系统界面  五.IntentFilter匹配 一.IntentFlag 复制一段内容    来源 http://i ...