1 public class Cutimg
2 {
3 /// <summary>
4 ///
5 /// </summary>
6 /// <param name="pPath">原图地址</param>
7 /// <param name="filePath">存储图片地址</param>
8 /// <param name="pPartStartPointX">截取目标-x</param>
9 /// <param name="pPartStartPointY">截取目标-y</param>
10 /// <param name="pPartWidth">截取目标-宽度</param>
11 /// <param name="pPartHeight">截取目标-高度</param>
12 /// <param name="pOrigStartPointX">缩放图-x:0</param>
13 /// <param name="pOrigStartPointY">缩放图-y:0</param>
14 /// <param name="imageWidth">缩放图-宽度</param>
15 /// <param name="imageHeight">缩放图-高度</param>
16 /// <returns></returns>
17 public static bool SaveCutPic(string pPath, string filePath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY, int imageWidth, int imageHeight)
18 {
19 using (System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath))
20 {
21 try
22 {
23 if (originalImg.Width == imageWidth && originalImg.Height == imageHeight)
24 {
25 return SaveCutPic(pPath, filePath, pPartStartPointX, pPartStartPointY, pPartWidth, pPartHeight,
26 pOrigStartPointX, pOrigStartPointY);
27
28 }
29
30 Bitmap thumimg = MakeThumbnail(originalImg, imageWidth, imageHeight);
31
32 Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
33
34 Graphics graphics = Graphics.FromImage(partImg);
35 Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
36 Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
37
38 ///文字水印
39 Graphics G = Graphics.FromImage(partImg);
40 //Font f = new Font("Lucida Grande", 6);
41 //Brush b = new SolidBrush(Color.Gray);
42 G.Clear(Color.White);
43 // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
44 G.InterpolationMode = InterpolationMode.HighQualityBicubic;
45 // 指定高质量、低速度呈现。
46 G.SmoothingMode = SmoothingMode.HighQuality;
47
48 graphics.DrawImage(thumimg, destRect, origRect, GraphicsUnit.Pixel);
49 //G.DrawString("Xuanye", f, b, 0, 0);
50 G.Dispose();
51
52 originalImg.Dispose();
53 if (File.Exists(filePath))
54 {
55 File.SetAttributes(filePath, FileAttributes.Normal);
56 File.Delete(filePath);
57 }
58 if (!Directory.Exists(Path.GetDirectoryName(filePath)))
59 {
60 Directory.CreateDirectory(Path.GetDirectoryName(filePath));
61
62 }
63 partImg.Save(filePath, ImageFormat.Jpeg);
64
65 partImg.Dispose();
66 thumimg.Dispose();
67 }
68 catch
69 {
70 return false;
71 }
72 return true;
73 }
74 }
75
76 public static Bitmap MakeThumbnail(System.Drawing.Image fromImg, int width, int height)
77 {
78 Bitmap bmp = new Bitmap(width, height);
79 int ow = fromImg.Width;
80 int oh = fromImg.Height;
81
82 //新建一个画板
83 Graphics g = Graphics.FromImage(bmp);
84
85 //设置高质量插值法
86 g.InterpolationMode = InterpolationMode.High;
87 //设置高质量,低速度呈现平滑程度
88 g.SmoothingMode = SmoothingMode.Default;
89 //清空画布并以透明背景色填充
90 g.Clear(Color.White);
91
92 g.DrawImage(fromImg, new Rectangle(0, 0, width, height),
93 new Rectangle(0, 0, ow, oh),
94 GraphicsUnit.Pixel);
95
96 return bmp;
97
98 }
99
100 public static bool SaveCutPic(string pPath, string filePath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
101 {
102 using (System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath))
103 {
104 try
105 {
106 Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
107 Graphics graphics = Graphics.FromImage(partImg);
108 Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
109 Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
110
111 ///注释 文字水印
112 Graphics G = Graphics.FromImage(partImg);
113 //Font f = new Font("Lucida Grande", 6);
114 //Brush b = new SolidBrush(Color.Gray);
115 G.Clear(Color.White);
116 // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
117 G.InterpolationMode = InterpolationMode.HighQualityBicubic;
118 // 指定高质量、低速度呈现。
119 G.SmoothingMode = SmoothingMode.HighQuality;
120
121 graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
122 //G.DrawString("Xuanye", f, b, 0, 0);
123 G.Dispose();
124
125 originalImg.Dispose();
126 if (File.Exists(filePath))
127 {
128 File.SetAttributes(filePath, FileAttributes.Normal);
129 File.Delete(filePath);
130 }
131 partImg.Save(filePath, ImageFormat.Jpeg);
132 partImg.Dispose();
133 }
134 catch
135 {
136 return false;
137 }
138 }
139 return true;
140 }
141 }