JavaScript html js图片切割系统,裁剪,图片处理

来源:互联网 发布:云计算安全解决 编辑:程序博客网 时间:2024/05/29 04:47

图片切割(裁剪),这里需要声明一下:

首先js是不能操作客户端文件的(除非特殊情况),所以图片的切割必须在后台处理,对于客户端的图片可以先上传再切割或者把图片和切割参数一起传递到服务器再处理(上传文件不在本次讨论范围,请自行修改);
还有是通过客户端传递的参数来处理图片,确实能得到展示图或预览图的效果(这是针对有些提出在后台处理的图片得不到预览图的效果来说的),下面会举例说明如何生成展示图那样的图片。

【客户端部分】

客户端部分详细请参考图片切割效果(建议没有看过的先看那里的例子),这里说说要传递到后台的参数,建议用GetPos获取部分参数:

Js代码
  1. var p = ic.Url, o = ic.GetPos();   
  2. x = o.Left,   
  3. y = o.Top,   
  4. w = o.Width,   
  5. h = o.Height,   
  6. pw = ic._layBase.width,   
  7. ph = ic._layBase.height;  



其中,ic.Url是图片地址:ic.Url,o.Left是水平切割,o.Top是垂直切割点,o.Width是切割宽度,o.Height是切割高度,ic._layBase.width是图片宽度,ic._layBase.height是图片高度。

可以这样传递这些参数:

Js代码
  1. $("imgCreat").src = "ImgCropper.ashx?p=" + p + "&x=" + x + "&y=" + y + "&w=" + w + "&h=" + h + "&pw=" + pw + "&ph=" + ph + "&" + Math.random();  



其中图片地址、图片宽度、图片高度如果是预先设定好的话就可以不传递了。

【后台部分】

后台主要是进行图片的处理和输出。

【图片切割】

在后台获取前台传递的参数:

C#代码
  1. string Pic = Convert.ToString(context.Request["p"]);   
  2. int PointX = Convert.ToInt32(context.Request["x"]);   
  3. int PointY = Convert.ToInt32(context.Request["y"]);   
  4. int CutWidth = Convert.ToInt32(context.Request["w"]);   
  5. int CutHeight = Convert.ToInt32(context.Request["h"]);   
  6. int PicWidth = Convert.ToInt32(context.Request["pw"]);   
  7. int PicHeight = Convert.ToInt32(context.Request["ph"]);  



然后就用这些参数对图片进行切割了,先说说切割的原理,主要分两部:切割和缩放。

切割和缩放的程序关键在这里:

C#代码
  1. gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight, GraphicsUnit.Pixel);  



首先是在原图切割出需要的部分。切割需要的参数是PointX、PointY、CutWidth、CutHeight,因为在客户端的程序中是可以缩放原图来进行切割的,所以要在在切割原图时需要先按比例缩放这些参数,还要注意PointX和CutWidth需要水平方向的比例,PointY和CutHeight需要垂直方向的比例。例如(其中imgPhoto是原图):
水平切割点:PointX * imgPhoto.Width / PicWidth;
切割宽度:CutWidth * imgPhoto.Width / PicWidth;
垂直切割点:PointY * imgPhoto.Height / PicHeight;
切割高度:CutHeight * imgPhoto.Height / PicHeight。

用这些参数就可以对原图进行切割了。

然后是缩放原图。可以想象,当切割好原图后,只要把图宽高缩放到CutWidth和CutHeight就可以得到跟展示图一样的图片了:

C#代码
  1. new Rectangle(0, 0, CutWidth, CutHeight),   



下面是处理图片的程序:

C#代码
  1. public MemoryStream ResetImg(string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)   
  2.     {   
  3.         Image imgPhoto = Image.FromFile(ImgFile);   
  4.         Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);   
  5.   
  6.         Graphics gbmPhoto = Graphics.FromImage(bmPhoto);   
  7.         gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight, GraphicsUnit.Pixel);   
  8.   
  9.         MemoryStream ms2 = new MemoryStream();   
  10.         bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);   
  11.   
  12.         imgPhoto.Dispose();   
  13.         gbmPhoto.Dispose();   
  14.         bmPhoto.Dispose();   
  15.   
  16.         return ms2;   
  17.     }  




当然对于不同的需求可能要生成的效果也不一样,但只要能灵活运用对于一般的需求基本都能实现了。
如果需要保存图片到服务器,那么可以用下面的方法保存图片:

C#代码
  1. bmPhoto.Save(文件物理路径, System.Drawing.Imaging.ImageFormat.Jpeg);  



【IHttpHandler】

程序通过ashx文件用IHttpHandler发送切割生成的图片,参考使用.ashx文件处理IHttpHandler实现发送文本及二进制数据的方法:
“利用.ashx文件是一个更好的方法,这个文件类似于.aspx文件,可以通过它来调用HttpHandler类,从而免去了普通.aspx页面的控件解析以及页面处理的过程。这个文件特别适合于生成动态图片,生成动态文本等内容。”

最后设置数据输出类型context.Response.ContentType,并使用context.Response.OutputStream向客户端输出图片数据:

C#代码
  1. context.Response.ContentType = "image/jpeg";   
  2. ResetImg(System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream);  




这个输出数据的方法适合用在不需要回发的数据输出中,例如ajax、动态图片等,关于这方面更详细的内容请看IHttpHandler接口。

下面是完整服务器端代码:

C#代码
  1. <%@ WebHandler Language="c#" Class="ImgCropper_WebHandler" Debug="true" %>   
  2.   
  3. using System;   
  4. using System.Web;   
  5. using System.Drawing;   
  6. using System.IO;   
  7.   
  8. public class ImgCropper_WebHandler : IHttpHandler   
  9. {   
  10.     public void ProcessRequest(HttpContext context)   
  11.     {   
  12.         string Pic = Convert.ToString(context.Request["p"]);   
  13.         int PointX = Convert.ToInt32(context.Request["x"]);   
  14.         int PointY = Convert.ToInt32(context.Request["y"]);   
  15.         int CutWidth = Convert.ToInt32(context.Request["w"]);   
  16.         int CutHeight = Convert.ToInt32(context.Request["h"]);   
  17.         int PicWidth = Convert.ToInt32(context.Request["pw"]);   
  18.         int PicHeight = Convert.ToInt32(context.Request["ph"]);   
  19.   
  20.         context.Response.ContentType = "image/jpeg";   
  21.         ResetImg(System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream);   
  22.     }   
  23.        
  24.     public MemoryStream ResetImg(string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)   
  25.     {   
  26.         Image imgPhoto = Image.FromFile(ImgFile);   
  27.         Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);   
  28.   
  29.         Graphics gbmPhoto = Graphics.FromImage(bmPhoto);   
  30.         gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight, GraphicsUnit.Pixel);   
  31.   
  32.         MemoryStream ms2 = new MemoryStream();   
  33.         bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);   
  34.   
  35.         imgPhoto.Dispose();   
  36.         gbmPhoto.Dispose();   
  37.         bmPhoto.Dispose();   
  38.   
  39.         return ms2;   
  40.     }   
  41.   
  42.   
  43.     public bool IsReusable   
  44.     {   
  45.         get  
  46.         {   
  47.             return false;   
  48.         }   
  49.     }   
  50. }  



由于涉及后台操作,请下载完整实例测试。


ps:实例中没有加入图片,测试时请自己插入一张图片(默认是1.jpg)。
ps2:应要求把asp版本加上了,但需要支持aspjpeg组件。