A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈君 金牌黑马   /  2014-8-4 17:06  /  950 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

转自:http://www.jb51.net/article/52829.htm
本文实例讲述了基于.net实现裁剪网站上传图片的方法。由于客户端Javascript不能操作文件,所以只能先上传图片再在服务器端剪切。
1、上传图片
2、Javascript剪切图片(其实只是选取要剪切的部分)
3、服务器端剪切

(1)在页面的cs文件中剪切。须放几个隐藏控件以便回传js选取的坐标。

其中剪切图片源码如下:
  1. using System;

  2. 02 using System.Collections.Generic;

  3. 03 using System.Text;

  4. 04 using System.Drawing;

  5. 05

  6. 06 public class Cut

  7. 07 {

  8. 08 /// <summary>

  9. 09 /// 裁剪图片

  10. 10 /// </summary>

  11. 11 /// <param name="sourceImg">原图片路径</param>

  12. 12 /// <param name="desImg">裁剪图片路径</param>

  13. 13 /// <param name="left">X</param>

  14. 14 /// <param name="top">Y</param>

  15. 15 /// <param name="width">宽</param>

  16. 16 /// <param name="height">高</param>

  17. 17 public static void CutImage(string sourceImg, string desImg, int left, int top, int width, int height)

  18. 18 {

  19. 19 System.Drawing.Image img = System.Drawing.Bitmap.FromFile(sourceImg);

  20. 20 System.Drawing.Image imgToSave = new System.Drawing.Bitmap(width, height);

  21. 21 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgToSave);

  22. 22 RectangleF sourceRect = new RectangleF(left, top, width, height);

  23. 23 RectangleF destinationRect = new RectangleF(0, 0, width, height);

  24. 24

  25. 25 g.DrawImage(img,

  26. 26 destinationRect,

  27. 27 sourceRect,

  28. 28 GraphicsUnit.Pixel

  29. 29 );

  30. 30 g.Save();

  31. 31 imgToSave.Save(desImg, System.Drawing.Imaging.ImageFormat.Jpeg);

  32. 32 g.Dispose();

  33. 33 imgToSave.Dispose();

  34. 34 img.Dispose();

  35. 35 }

  36. 36

  37. 37

  38. 38 }
复制代码

(2)在ashx中剪切,可回传文件流。用参数传递坐标。

  1. <DIV class=blockcode>
  2. <BLOCKQUOTE>using System;

  3. 02 using System.Web;

  4. 03 using System.Drawing;

  5. 04 using System.IO;

  6. 05

  7. 06 public class ImgCropper_WebHandler : IHttpHandler

  8. 07 {

  9. 08 public void ProcessRequest(HttpContext context)

  10. 09 {

  11. 10 string Pic = Convert.ToString(context.Request["p"]);

  12. 11 int PointX = Convert.ToInt32(context.Request["x"]);

  13. 12 int PointY = Convert.ToInt32(context.Request["y"]);

  14. 13 int CutWidth = Convert.ToInt32(context.Request["w"]);

  15. 14 int CutHeight = Convert.ToInt32(context.Request["h"]);

  16. 15 int PicWidth = Convert.ToInt32(context.Request["pw"]);

  17. 16 int PicHeight = Convert.ToInt32(context.Request["ph"]);

  18. 17

  19. 18 context.Response.ContentType = "image/jpeg";

  20. 19 ResetImg(context, System.Web.HttpContext.Current.Server.MapPath(Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(context.Response.OutputStream);

  21. 20 }

  22. 21

  23. 22 public MemoryStream ResetImg(HttpContext context, string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight)

  24. 23 {

  25. 24 Image imgPhoto = Image.FromFile(ImgFile);

  26. 25 Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

  27. 26

  28. 27 Graphics gbmPhoto = Graphics.FromImage(bmPhoto);

  29. 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);

  30. 29

  31. 30 //保存图片到服务器

  32. 31 bmPhoto.Save(context.Server.MapPath("upload/") + Guid.NewGuid() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

  33. 32

  34. 33 //生成文件流回传

  35. 34 MemoryStream ms2 = new MemoryStream();

  36. 35 bmPhoto.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);

  37. 36

  38. 37 imgPhoto.Dispose();

  39. 38 gbmPhoto.Dispose();

  40. 39 bmPhoto.Dispose();

  41. 40

  42. 41 return ms2;

  43. 42 }

  44. 43

  45. 44

  46. 45 public bool IsReusable

  47. 46 {

  48. 47 get

  49. 48 {

  50. 49 return false;

  51. 50 }

  52. 51 }

  53. 52 }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马