jQuery Jcrop外掛程式實現圖片選取功能

來源:互聯網
上載者:User

總的來說,原理很簡單,大致流程是:在瀏覽器上載入原圖 --> 用矩形框在原圖上選取地區並將選取的頂點座標和矩形尺寸發送至伺服器 --> 伺服器端用圖片切割演算法切割原圖並輸出切割後的圖片。下面我們就分別對這幾個步驟詳細展開討論分析,並在最後附上小demo供大家參考。

1、在頁面上載入原圖

這個就不用多說了,就是在頁面上顯示一張圖片,一個img標籤搞定,不過為了下一步示範,還是貼一下代碼
<img src="girl.jpg" alt="" id="TestImage" style="float: left;">

2、用矩形框在原圖上選取地區

這個我們要用到一個jQuery外掛程式Jcrop,感謝Jcrop,其項目頁面地址:http://deepliquid.com/content/Jcrop.html,再次感謝。接下來就是運用這個外掛程式來讓我們能在原圖上隨意地切圖。先在頁面上放幾個隱藏欄位,用來存放當前選取的頂點座標及選取矩形的尺寸,再放一個儲存按鈕,點擊儲存按鈕後將在伺服器上儲存切割後的圖片。代碼如下: 複製代碼 代碼如下:<form id="AvatarForm" action="">
<input id="x" name="x" type="hidden">
<input id="y" name="y" type="hidden">
<input id="w" name="w" type="hidden">
<input id="h" name="h" type="hidden">
<input class="input_btn" id="BtnSaveAvatar" value="確定儲存" type="submit">
</form>

四個隱藏欄位分別表示x:左上頂點x座標;y:左上頂點y座標;w:選取矩形寬度;h:選取矩形長度。
然後我們調用外掛程式,做好初始化工作,引入js和css檔案: 複製代碼 代碼如下:<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript"src="js/Jcrop/js/jquery.Jcrop.js"></script>
<link rel="stylesheet" href="js/Jcrop/css/jquery.Jcrop.css" type="text/css">

初始化原圖的js代碼: 複製代碼 代碼如下:$(document).ready(function(){
$('#TestImage').Jcrop({
onChange: updateCoords,
onSelect: updateCoords
});
$("#BtnSaveAvatar").click(function(){
$.ajax({
url:'Handler.ashx',
data:{'x':$("#x").val(),'y':$("#y").val(),'w':$("#w").val(),'h':$("#h").val()},
datatype : "text/json",
type:'post',
success: function(o){ window.location.href="result.aspx?url="+o;} //成功後跳轉到result頁面查看切割後圖片,把url
});
return false;
});
});
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);
context.Response.Write(savePath); //輸出儲存的路徑,以便頁面端接收路徑顯示切割後的圖片
}

最後我們在Result.aspx頁面上接收切割後的圖片路徑並顯示切割後的圖片:

<img src="<%=Request["url"] %>" alt="" />
好了,整個過程就完成了,為了大家能更好的參考學習,下面附上小demo,下載demo。

最後,你是否帶走了我留下的這片雲彩?請告訴我,我很期待你的答案。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.