發(fā)表日期:2015-09-13 文章編輯:南昌開優(yōu)網(wǎng)絡(luò) 瀏覽次數(shù):4554 標簽:ASP.NET應(yīng)用,jQuery應(yīng)用
<form id="form1" runat="server"> <input type="hidden" id="x" name="x" /> <input type="hidden" id="y" name="y" /> <input type="hidden" id="w" name="w" /> <input type="hidden" id="h" name="h" /> <table class="style1"> <tr> <td> </td> <td> <asp:FileUpload ID="fuimg" runat="server" /> <asp:Button ID="btnUpload" runat="server" Text="上傳" OnClick="btnUpload_Click" /> </td> </tr> <tr> <td> </td> <td> <asp:Image ID="img" runat="server" /> </td> </tr> <tr> <td> <asp:HiddenField ID="hfimg" runat="server" /> </td> <td> <input type="submit" class="input_btn" id="BtnSaveAvatar" value="確定保存" />選取圖片后點擊保存查看切割后圖片 </td> </tr> </table> <script type="text/javascript"> $(document).ready(function () { $('#<%=img.ClientID %>').Jcrop({ //img元素執(zhí)行Jcrop方法 onChange: updateCoords, //調(diào)用下面的updateCoords方法 onSelect: updateCoords }); var img = $("#<%=hfimg.ClientID %>").val(); //找到隱藏域 $("#BtnSaveAvatar").click(function () { //確定保存的單擊事件 $.ajax({ url: "Handler.ashx?img=" + img + "", //傳入到Handler一般處理程序中 并把當前圖片的url地址傳入進去 data: { 'x': $("#x").val(), 'y': $("#y").val(), 'w': $("#w").val(), 'h': $("#h").val() }, //傳入的數(shù)據(jù) datatype: "text/json", //json方法 type: 'post', //post提交 success: function (o) { window.location.href = "default.aspx?url=" + o; } //回調(diào)函數(shù) }); return false; }); }); function updateCoords(c) { $('#x').val(c.x); $('#y').val(c.y); $('#w').val(c.w); $('#h').val(c.h); }; </script> <img src="<%=Request["url"] %>" alt="" /> //返回回調(diào)函數(shù)的新圖片的url地址 </form>
string xstr = context.Request["x"]; string ystr = context.Request["y"]; string wstr = context.Request["w"]; string hstr = context.Request["h"]; string img = context.Request.QueryString["img"]; //獲取傳入的img的url地址 string[] ss = img.Split('.'); //分割 string sourceFile = context.Server.MapPath("~/upload/" + img + ""); //保存到 string datedir = DateTime.Now.ToString("yyyyMMdd"); //創(chuàng)建日期文件夾 if (!Directory.Exists(datedir)) { Directory.CreateDirectory(datedir); } string savePath = "upload/" + datedir + "/" + Guid.NewGuid() + "." + ss[1]; //新文件保存到upload+當前日期+ 隨機字符 + 傳入圖片的后綴(如: "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); //輸出新圖片的地址。通過回調(diào)函數(shù)獲取并返回
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; }