实现图像剪裁 jquery.Jcrop



  配合 jquery.Jcrop 实现上传图片进行剪裁保存功能

  
    <script src="js/jquery.min.js"></script>
<script src="js/jquery.Jcrop.js"></script>
<link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
<script type="text/javascript">
jQuery(function ($) {
//target是<img>的id
jQuery('#target').Jcrop({
onChange: showCoords,
onSelect: showCoords
});
}); function showCoords(c) {
jQuery('#x').val(c.x); //x起点坐标
jQuery('#y').val(c.y);
jQuery('#x2').val(c.x2);//x终点坐标
jQuery('#y2').val(c.y2);
jQuery('#w').val(c.w);
jQuery('#h').val(c.h);
};
</script>


后台代码
        /// <summary>
/// 剪裁图像
/// </summary>
/// <param name="Img">原图物理地址</param>
/// <param name="Width">新图宽度</param>
/// <param name="Height">新图高度</param>
/// <param name="X">绘制起点X轴</param>
/// <param name="Y">绘制起点Y轴</param>
/// <returns></returns>
private byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
try
{
using (var OriginalImage = new Bitmap(Img))
{
using (var bmp = new Bitmap(Width, Height, OriginalImage.PixelFormat))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;//设置高质量,低速度呈现平滑程度
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;//设置高质量插值法
Graphic.Clear(Color.Transparent);//清空画布并以透明背景色填充
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height,
GraphicsUnit.Pixel);
方法一
bmp.Save(Server.MapPath("上传裁剪") + "new.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//图片另存
方法二 //转换二进制流
var ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat); return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw (Ex);
}
}

上一篇:UITableView中cell点击的绚丽动画效果


下一篇:jQuery Jcrop API参数说明(中文版)(转)(图片剪切)