C# 实现图片任意裁剪以及羽化功能
------解决思路----------------------
/// <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 static Bitmap CutImage(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX >= w
------解决思路----------------------
StartY >= h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}
}
------解决思路----------------------
图片在线裁剪示例
<script type="text/javascript">
$(function() { $('#oImage').Jcrop({ boxWidth: 520, boxHeight: 330, onChange: showCoords, onSelect: showCoords }); }); //当选择、改变选区时都执行showCoords函数
function showCoords(c) {
$("#txtX").val(c.x);//得到选中区域左上角横坐标
$("#txtY").val(c.y);//得到选中区域左上角纵坐标
$("#txtW").val(c.w);//得到选中区域的宽度
$("#txtH").val(c.h); //得到选中区域的高度
}//codego.net/
function checkCoords() {
var defaulturl=document.getElementById("oImage").src;
defaulturl=defaulturl.substring(defaulturl.lastIndexOf("/")+1);
if (defaulturl == "default1.jpg") {
alert("请上传图片");
return false;
}
else {
if (parseInt($('#txtH').val()) && parseInt($('#txtW').val())) {
sendImg();
return true;
}
else {
alert("请设置裁剪区域");
return false;
}
}
};
function sendImg() {
var p = document.getElementById("oImage").src;
var x = document.getElementById("txtX").value;
var y = document.getElementById("txtY").value;
var w = document.getElementById("txtW").value;
var h = document.getElementById("txtH").value;
var ow = 222;
var oh = 300;
var rate = w / h;
if (rate > 1)//选区的宽大于高
{
document.getElementById("imgCreat").width = ow;
document.getElementById("imgCreat").height = ow / rate;
}
else if (rate < 1) //选区的高大于宽
{
document.getElementById("imgCreat").width = oh * rate;
document.getElementById("imgCreat").height = oh;
}
else if (rate == 1) {
document.getElementById("imgCreat").width = 222;
document.getElementById("imgCreat").height =222;
}
document.getElementById("imgCreat").src = "Handler.ashx?p=" + p + "&x=" + x + "&y=" + y + "&w=" + w + "&h=" + h + "&" + Math.random();
}
</script>