C#圖片處理---基礎

簡單的圖片剪裁ide

  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 經過文件路徑圖片 public static Image ImgCutFromFile
 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             
 29             Image img = Image.FromFile(imgFile);
 30             if (widthS > 0 && heightS > 0)
 31             {
 32                 GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img);
 33             }
 34             return ImgCut(img, startX, startY, width, height);
 35         }
 36         #endregion
 37 
 38         #region 經過流圖片 public static Image ImgCutFromStream
 39         /// <summary>
 40         /// 經過流圖片
 41         /// </summary>
 42         /// <param name="stream"></param>
 43         /// <param name="startX">圖片左上角X座標</param>
 44         /// <param name="startY">圖片左上角Y座標</param>
 45         /// <param name="width">截取寬度</param>
 46         /// <param name="height">截取高度</param>
 47         /// <param name="widthS">圖片顯示寬度</param>
 48         /// <param name="heightS">圖片顯示高度</param>
 49         /// <returns></returns>
 50         public static Image ImgCutFromStream(Stream stream, int startX, int startY, int width, int height, int widthS = 0, int heightS = 0)
 51         {
 52             Image img = Image.FromStream(stream);
 53             if (widthS > 0 && heightS > 0)
 54             {
 55                 GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img);
 56             }
 57             return ImgCut(img, startX, startY, width, height);
 58         }
 59         #endregion
 60 
 61         #region 獲取實際XYWH private static void GetXYWH
 62         /// <summary>
 63         /// 獲取實際XYWH
 64         /// </summary>
 65         /// <param name="startX">圖片左上角X座標</param>
 66         /// <param name="startY">圖片左上角Y座標</param>
 67         /// <param name="width">截取寬度</param>
 68         /// <param name="height">截取高度</param>
 69         /// <param name="widthS">圖片顯示寬度</param>
 70         /// <param name="heightS">圖片顯示高度</param>
 71         /// <param name="img"></param>
 72         private static void GetXYWH(ref int startX, ref int startY, ref int width, ref int height, int widthS, int heightS, Image img)
 73         {
 74             /*img.Width / widthS 是實際圖片大小與頁面顯示圖片大小比值*/
 75             startX = Convert.ToInt32(startX * img.Width / widthS);
 76             startY = Convert.ToInt32(startY * img.Height / heightS);
 77             width = Convert.ToInt32(width * img.Width / widthS);
 78             height = Convert.ToInt32(height * img.Height / heightS);
 79         }
 80         #endregion
 81 
 82         #region 執行剪裁 public static Image ImgCut
 83         /// <summary>
 84         /// 圖片裁剪
 85         /// </summary>
 86         /// <param name="image">圖片</param>
 87         /// <param name="startX">圖片左上角X座標</param>
 88         /// <param name="startY">圖片左上角Y座標</param>
 89         /// <param name="width">截取寬度</param>
 90         /// <param name="height">截取高度</param>
 91         /// <returns></returns>
 92         public static Image ImgCut(Image image, int startX, int startY, int width, int height)
 93         {
 94             Image img = image;//獲取原始圖片
 95             Image newImg = new Bitmap(width, height);//裁剪後的圖片
 96             Graphics g = Graphics.FromImage(newImg);
 97             /*設置質量*/
 98             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 99             g.SmoothingMode = SmoothingMode.HighQuality;
100             /*圖片源位置,圖片源大小*/
101             Rectangle fromR = new Rectangle(startX, startY, width, height);
102             /*繪圖位置繪圖大小*/
103             Rectangle toR = new Rectangle(0, 0, width, height);
104             /*畫圖*/
105             g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
106             /*釋放截圖資源*/
107             g.Dispose();
108             return newImg;
109             //newImg.Save(Server.MapPath(""));
110             //newImg.Dispose();
111         }
112         #endregion
113         #endregion
114 
115         #region 改變圖片大小 public static Image ImgSize
116         /// <summary>
117         /// 改變圖片大小
118         /// </summary>
119         /// <param name="image">圖片</param>
120         /// <param name="width">新寬度</param>
121         /// <param name="height">新高度</param>
122         /// <returns></returns>
123         public static Image ImgSize(Image image, int width = 0, int height = 0)
124         {
125             Image img = image;//獲取原始圖片
126             Image newImg = new Bitmap(width, height);//裁剪後的圖片
127             Graphics g = Graphics.FromImage(newImg);
128             /*設置質量*/
129             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
130             g.SmoothingMode = SmoothingMode.HighQuality;
131             /*圖片源位置,圖片源大小*/
132             Rectangle fromR = new Rectangle(0, 0, img.Width, img.Height);
133             /*繪圖位置繪圖大小*/
134             if (width == 0 && height == 0) {
135                 width = img.Width;
136                 height = img.Height;
137             }
138             Rectangle toR = new Rectangle(0, 0, width, height);
139             /*畫圖*/
140             g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
141             /*釋放截圖資源*/
142             g.Dispose();
143             return newImg;
144         }
145         #endregion
146 
147     }
148 }
149 
150 Img
View Code
相關文章
相關標籤/搜索