转自:http://www.jb51.net/article/52829.htm
本文实例讲述了基于.net实现裁剪网站上传图片的方法。由于客户端Javascript不能操作文件,所以只能先上传图片再在服务器端剪切。
1、上传图片
2、Javascript剪切图片(其实只是选取要剪切的部分)
3、服务器端剪切
(1)在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。
其中剪切图片源码如下:
- using System;
- 02 using System.Collections.Generic;
- 03 using System.Text;
- 04 using System.Drawing;
- 05
- 06 public class Cut
- 07 {
- 08 /// <summary>
- 09 /// 裁剪图片
- 10 /// </summary>
- 11 /// <param name="sourceImg">原图片路径</param>
- 12 /// <param name="desImg">裁剪图片路径</param>
- 13 /// <param name="left">X</param>
- 14 /// <param name="top">Y</param>
- 15 /// <param name="width">宽</param>
- 16 /// <param name="height">高</param>
- 17 public static void CutImage(string sourceImg, string desImg, int left, int top, int width, int height)
- 18 {
- 19 System.Drawing.Image img = System.Drawing.Bitmap.FromFile(sourceImg);
- 20 System.Drawing.Image imgToSave = new System.Drawing.Bitmap(width, height);
- 21 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgToSave);
- 22 RectangleF sourceRect = new RectangleF(left, top, width, height);
- 23 RectangleF destinationRect = new RectangleF(0, 0, width, height);
- 24
- 25 g.DrawImage(img,
- 26 destinationRect,
- 27 sourceRect,
- 28 GraphicsUnit.Pixel
- 29 );
- 30 g.Save();
- 31 imgToSave.Save(desImg, System.Drawing.Imaging.ImageFormat.Jpeg);
- 32 g.Dispose();
- 33 imgToSave.Dispose();
- 34 img.Dispose();
- 35 }
- 36
- 37
- 38 }
复制代码
(2)在ashx中剪切,可回传文件流。用参数传递坐标。
- <DIV class=blockcode>
- <BLOCKQUOTE>using System;
- 02 using System.Web;
- 03 using System.Drawing;
- 04 using System.IO;
- 05
- 06 public class ImgCropper_WebHandler : IHttpHandler
- 07 {
- 08 public void ProcessRequest(HttpContext context)
- 09 {
- 10 string Pic = Convert.ToString(context.Request["p"]);
- 11 int PointX = Convert.ToInt32(context.Request["x"]);
- 12 int PointY = Convert.ToInt32(context.Request["y"]);
- 13 int CutWidth = Convert.ToInt32(context.Request["w"]);
- 14 int CutHeight = Convert.ToInt32(context.Request["h"]);
- 15 int PicWidth = Convert.ToInt32(context.Request["pw"]);
- 16 int PicHeight = Convert.ToInt32(context.Request["ph"]);
- 17
- 18 context.Response.ContentType = "image/jpeg";
- 19 ResetImg(context, System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream);
- 20 }
- 21
- 22 public MemoryStream ResetImg(HttpContext context, string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)
- 23 {
- 24 Image imgPhoto = Image.FromFile(ImgFile);
- 25 Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
- 26
- 27 Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
- 28 gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * imgPhoto.Width / PicWidth, PointY * imgPhoto.Height / PicHeight, CutWidth * imgPhoto.Width / PicWidth, CutHeight * imgPhoto.Height / PicHeight, GraphicsUnit.Pixel);
- 29
- 30 //保存图片到服务器
- 31 bmPhoto.Save(context.Server.MapPath("upload/") + Guid.NewGuid() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
- 32
- 33 //生成文件流回传
- 34 MemoryStream ms2 = new MemoryStream();
- 35 bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
- 36
- 37 imgPhoto.Dispose();
- 38 gbmPhoto.Dispose();
- 39 bmPhoto.Dispose();
- 40
- 41 return ms2;
- 42 }
- 43
- 44
- 45 public bool IsReusable
- 46 {
- 47 get
- 48 {
- 49 return false;
- 50 }
- 51 }
- 52 }
复制代码
|
|