首页 > 代码库 > 裁剪图片
裁剪图片
1 using System; 2 using System.Drawing; 3 using System.Drawing.Drawing2D; 4 using System.IO; 5 6 namespace ClassLibrary 7 { 8 /// <summary> 9 /// 图片处理 10 /// </summary> 11 public static class Img 12 { 13 #region 图片裁剪 14 #region 通过文件路径图片 15 /// <summary> 16 /// 通过文件路径裁剪图片 17 /// </summary> 18 /// <param name="imgFile">文件路径</param> 19 /// <param name="startX">图片左上角X坐标</param> 20 /// <param name="startY">图片左上角Y坐标</param> 21 /// <param name="width">截取宽度</param> 22 /// <param name="height">截取高度</param> 23 /// <param name="widthS">图片显示宽度</param> 24 /// <param name="heightS">图片显示高度</param> 25 /// <returns></returns> 26 public static Image ImgCutFromFile(string imgFile, int startX, int startY, int width, int height, int widthS = 0, int heightS=0) 27 { 28 Image img = Image.FromFile(imgFile); 29 if (widthS > 0 && heightS > 0) 30 { 31 GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img); 32 } 33 return ImgCut(img, startX, startY, width, height); 34 } 35 #endregion 36 37 #region 通过流图片 38 /// <summary> 39 /// 通过流图片 40 /// </summary> 41 /// <param name="stream">流</param> 42 /// <param name="startX">图片左上角X坐标</param> 43 /// <param name="startY">图片左上角Y坐标</param> 44 /// <param name="width">截取宽度</param> 45 /// <param name="height">截取高度</param> 46 /// <param name="widthS">图片显示宽度</param> 47 /// <param name="heightS">图片显示高度</param> 48 /// <returns></returns> 49 public static Image ImgCutFromStream(Stream stream, int startX, int startY, int width, int height, int widthS = 0, int heightS = 0) 50 { 51 Image img = Image.FromStream(stream); 52 if (widthS > 0 && heightS > 0) 53 { 54 GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img); 55 } 56 return ImgCut(img, startX, startY, width, height); 57 } 58 #endregion 59 60 #region 获取实际XYWH 61 /// <summary> 62 /// 获取实际XYWH 63 /// </summary> 64 /// <param name="startX">图片左上角X坐标</param> 65 /// <param name="startY">图片左上角Y坐标</param> 66 /// <param name="width">截取宽度</param> 67 /// <param name="height">截取高度</param> 68 /// <param name="widthS">图片显示宽度</param> 69 /// <param name="heightS">图片显示高度</param> 70 /// <param name="img"></param> 71 private static void GetXYWH(ref int startX, ref int startY, ref int width, ref int height, int widthS, int heightS, Image img) 72 { 73 /*img.Width / widthS 是实际图片大小与页面显示图片大小比值*/ 74 startX = Convert.ToInt32(startX * img.Width / widthS); 75 startY = Convert.ToInt32(startY * img.Height / heightS); 76 width = Convert.ToInt32(width * img.Width / widthS); 77 height = Convert.ToInt32(height * img.Height / heightS); 78 } 79 #endregion 80 81 #region 执行剪裁 82 /// <summary> 83 /// 图片裁剪 84 /// </summary> 85 /// <param name="image">图片</param> 86 /// <param name="startX">图片左上角X坐标</param> 87 /// <param name="startY">图片左上角Y坐标</param> 88 /// <param name="width">截取宽度</param> 89 /// <param name="height">截取高度</param> 90 /// <returns></returns> 91 public static Image ImgCut(Image image, int startX, int startY, int width, int height) 92 { 93 Image img = image;//获取原始图片 94 Image newImg = new Bitmap(width, height);//裁剪后的图片 95 Graphics g = Graphics.FromImage(newImg); 96 /*设置质量*/ 97 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 98 g.SmoothingMode = SmoothingMode.HighQuality; 99 /*图片源位置,图片源大小*/100 Rectangle fromR = new Rectangle(startX, startY, width, height);101 /*绘图位置绘图大小*/102 Rectangle toR = new Rectangle(0, 0, width, height);103 /*画图*/104 g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);105 /*释放截图资源*/106 g.Dispose();107 return newImg;108 //newImg.Save(Server.MapPath(""));109 //newImg.Dispose();110 }111 #endregion112 #endregion113 114 #region 改变图片大小115 /// <summary>116 /// 改变图片大小117 /// </summary>118 /// <param name="image">图片</param>119 /// <param name="width">新宽度</param>120 /// <param name="height">新高度</param>121 /// <returns></returns>122 public static Image ImgSize(Image image, int width = 0, int height = 0)123 {124 Image img = image;//获取原始图片125 Image newImg = new Bitmap(width, height);//裁剪后的图片126 Graphics g = Graphics.FromImage(newImg);127 /*设置质量*/128 g.InterpolationMode = InterpolationMode.HighQualityBicubic;129 g.SmoothingMode = SmoothingMode.HighQuality;130 /*图片源位置,图片源大小*/131 Rectangle fromR = new Rectangle(0, 0, img.Width, img.Height);132 /*绘图位置绘图大小*/133 if (width == 0 && height == 0) {134 width = img.Width;135 height = img.Height;136 }137 Rectangle toR = new Rectangle(0, 0, width, height);138 /*画图*/139 g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);140 /*释放截图资源*/141 g.Dispose();142 return newImg;143 }144 #endregion145 }146 }
裁剪图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。