jQuery Jcrop插件实现图片选取功能

2020-05-22 15:28:54易采站长站整理

});
});
function updateCoords(c){
$(‘#x’).val(c.x);
$(‘#y’).val(c.y);
$(‘#w’).val(c.w);
$(‘#h’).val(c.h);
};

经过上述步骤,我们很高兴的看到一位美女出现在我们眼前,并任由我们选取任何部位,很刺激吧,由于demo在最后,先在这截一张效果图吧


3、服务器端切割图片并输出切割后的图片:
切割图片的主要类代码如下:

public class ImageCut
{
///<summary>
/// 剪裁 — 用GDI+
///</summary>
///<param name=”b”>原始Bitmap</param>
///<param name=”StartX”>开始坐标X</param>
///<param name=”StartY”>开始坐标Y</param>
///<param name=”iWidth”>宽度</param>
///<param name=”iHeight”>高度</param>
///<returns>剪裁后的Bitmap</returns>
public Bitmap KiCut(Bitmap b)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (X >= w || Y >= h)
{
return null;
}
if (X + Width > w)
{
Width = w – X;
}
if (Y + Height > h)
{
Height = h – Y;
}
try
{
Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
public int X = 0;
public int Y = 0;
public int Width = 120;
public int Height = 120;
public ImageCut(int x, int y, int width, int heigth)
{
X = x;
Y = y;
Width = width;
Height = heigth;
}
}

在Handler.ashx中,接收页面传递过来的切割图片的顶点坐标以及长宽尺寸,并调用C#图像切割类实现图片切割:

public void ProcessRequest (HttpContext context) {
string xstr = context.Request[“x”];
string ystr = context.Request[“y”];
string wstr = context.Request[“w”];
string hstr = context.Request[“h”];
string sourceFile = context.Server.MapPath(“girl.jpg”);
string savePath = “CutImage/” + Guid.NewGuid() + “.jpg”;
int x = 0;
int y = 0;
int w = 1;
int h = 1;
try
{
x = int.Parse(xstr);
y = int.Parse(ystr);
w = int.Parse(wstr);
h = int.Parse(hstr);
}
catch { }
ImageCut ic = new ImageCut(x, y, w, h);
System.Drawing.Bitmap cuted = ic.KiCut(new System.Drawing.Bitmap(sourceFile));
string cutPath = context.Server.MapPath(savePath);
cuted.Save(cutPath, System.Drawing.Imaging.ImageFormat.Jpeg);