ASP.net入门篇 上传图片并生存裁剪图,废话不多说了,一起看代码了
View
代码
<link rel="stylesheet" href="http://www.cnblogs.com/Scripts/jquery.Jcrop.CSS" type="text/css" /> <script src="http://www.cnblogs.com/Scripts/jquery-1.3.2.min.js" type="text/JavaScript"></script> <script src="http://www.cnblogs.com/Scripts/jquery.Jcrop.js" type="text/Javascript"></script>
<script language="Javascript" type="text/javascript">
// Remember to invoke within jQuery(window).load(...) // If you don’t, Jcrop may not initialize properly jQuery(window).load(function() {
jQuery(’#cropbox’).Jcrop({ onChange: showPreview, onSelect: showPreview, aspectRatio: 0.7//矩形比例 0表示自由比例 });
});
// Our simple event handler, called from onChange and onSelect // event handlers, as per the Jcrop invocation above function showPreview(coords) { if (parseInt(coords.w) > 0) { var rx = 100 / coords.w; var ry = 143 / coords.h;
jQuery(’#preview’).css({ width: Math.round(rx * $("#cropbox").attr("width")) + ’px’, height: Math.round(ry * $("#cropbox").attr("height")) + ’px’, marginLeft: ’-’ + Math.round(rx * coords.x) + ’px’, marginTop: ’-’ + Math.round(ry * coords.y) + ’px’ });
jQuery(’#x’).val(coords.x); jQuery(’#y’).val(coords.y); jQuery(’#w’).val(coords.w); jQuery(’#h’).val(coords.h); } }
</script>
<img src="<%= ViewData["file"] %>" width="<% = ViewData["width"] %>" height = "<%= ViewData["height"]%>" id="cropbox" alt="原图"/>
<div style="width:100px;height:143px;overflow:hidden;"> <img src="<%= ViewData["file"] %>" id="preview" alt="预览" /> </div>
<form action="/photo.ashx" method="post"> <%= HTML.Hidden("isHandled", true)%> <%= Html.Hidden("fileName",ViewData["fileName"]) %> <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" /> <input type="submit" value="保存缩略图" /> </form>
<form action="/photo.ashx" method="post" enctype="multipart/form-data"> <input type="file" name="imageFile" id="imageFile" /> <input type="submit" value="保存文件" />
</form>
Controllers
代码 [HandleError] public class HomeController : Controller {
public ActionResult Index(string tempFile) { double ImageMaxPx = 300.0; if (tempFile == null) { tempFile = "flowers.jpg"; } ViewData["fileName"] = tempFile; string temp = Path.Combine(Server.MapPath("/userFiles"), tempFile); if (System.IO.File.Exists(temp)) { try { Image image = Image.FromFile(temp); int width = image.Width; int height = image.Height; image.Dispose(); if (width > ImageMaxPx || height > ImageMaxPx) { if (width > height) { height = (int)(ImageMaxPx / width * height); width = (int)ImageMaxPx; } else { width = (int)(ImageMaxPx / height * width); height = (int)ImageMaxPx; } } ViewData["file"] = string.Format("/userFiles/{0}", tempFile); ViewData["width"] = width; ViewData["height"] = height; } catch { } }
return View(); }
photo.ashx
代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.IO; using System.Drawing;
namespace MvcApplication1 { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class photo : IHttpHandler {
public void ProcessRequest(HttpContext context) {
if (context.Request["isHandled"] == null) {
HttpPostedFile file = context.Request.Files["imageFile"];
if (file != null) { string strFileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName).ToLower(); string path = Path.Combine(context.Server.MapPath("/userFiles"), strFileName); file.SaveAs(path); context.Response.Redirect("/Home/Index?tempFile=" + strFileName); } else { context.Response.Redirect("/Home/Index"); } } else {
try { double ImageMaxPx = 300.0; string strFileName = context.Request["fileName"]; int x; if (!int.TryParse(context.Request["x"], out x)) { x = 0; } int y; if (!int.TryParse(context.Request["y"], out y)) { y = 0; } int w; if (!int.TryParse(context.Request["w"], out w)) { w = 100; } int h; if (!int.TryParse(context.Request["h"],out h)) { h = 143; } Image orgImage = Image.FromFile(Path.Combine(context.Server.MapPath("/userFiles"), strFileName)); double r = 1.0;
if (orgImage.Width > ImageMaxPx || orgImage.Height > ImageMaxPx) { if (orgImage.Width > orgImage.Height) { r = ImageMaxPx / orgImage.Width; } else { r = ImageMaxPx / orgImage.Height; } } x = (int)(x / r); y = (int)(y / r); w = (int)(w / r); h = (int)(h / r);
Image currentImage = new Bitmap(100, 143, System.Drawing.Imaging.PixelFormat.Format32bppRgb); Graphics g = Graphics.FromImage(currentImage); Rectangle destRect = new Rectangle(0, 0, 100, 143); Rectangle srcRect = new Rectangle(x, y, w, h); g.DrawImage(orgImage, destRect, srcRect, GraphicsUnit.Pixel); g.Dispose(); orgImage.Dispose(); currentImage.Save(Path.Combine(context.Server.MapPath("/userFiles"), strFileName)); context.Response.Redirect("/Home/Index?tempFile=" + strFileName);
} catch { } } }
public bool IsReusable { get { return false; } } } }
中联无限科技公司提供专业的成都网站建设、成都网站设计、成都网站制作、成都网站推广。
上一篇:获取SQL Server数据字典的经典SQL语句
下一篇:c#正则表达式入门提升教程分享
|