C#文件操作简单封装

1 public class FileManager
2 {
3 public bool IsHidden(string path)
4 {
5 FileAttributes attr = File.GetAttributes(path);
6 if (attr.ToString().LastIndexOf("Hidden") != -1)
7 return true;
8 else
9 return false;
10 }
11
12 public void Mkdir(string dir)
13 {
14 DirectoryInfo info = new DirectoryInfo(dir);
15 if (!info.Exists)
16 info.Create();
17 }
18 /// <summary>
19 /// 删除文件及其所有目录
20 /// </summary>
21 /// <param name="dir"></param>
22 /// <param name="delSub"></param>
23   public static void DelAll(string dir, bool delSub)
24 {
25 foreach (string fileName in Directory.GetFiles(dir))
26 {
27 File.SetAttributes(fileName, FileAttributes.Normal);
28 File.Delete(fileName);
29 }
30 if (delSub)
31 {
32 DirectoryInfo dirInfo = new DirectoryInfo(dir);
33 foreach (DirectoryInfo sub in dirInfo.GetDirectories())
34 {
35 DelAll(sub.FullName, true);
36 }
37 }
38 }
39
40 public static void DelDir(string dir)
41 {
42 DirectoryInfo dirInfo = new DirectoryInfo(dir);
43 if (dirInfo.Exists)
44 {
45 DelAll(dir, true);
46 dirInfo.Delete(true);
47 }
48 }
49
50 public static void DelSubDir(string dir)
51 {
52 foreach (string subDir in Directory.GetDirectories(dir))
53 {
54 DelDir(subDir);
55 }
56 }
57
58 public static void MoveFiles(string src, string target, bool overWrite, bool moveSub)
59 {
60 foreach (string srcFileName in Directory.GetFiles(src))
61 {
62 string targetFileName = Path.Combine(target, srcFileName.Substring(srcFileName.LastIndexOf("\\" + 1)));
63 if (File.Exists(targetFileName))
64 {
65 if (overWrite)
66 {
67 File.SetAttributes(targetFileName, FileAttributes.Normal);
68 File.Delete(targetFileName);
69 File.Move(srcFileName, targetFileName);
70 }
71 else
72 {
73 File.Move(srcFileName, targetFileName);
74 }
75 }
76 if (moveSub)
77 {
78 foreach (string srcSubDir in Directory.GetDirectories(src))
79 {
80 string targetSubDir = Path.Combine(target, srcSubDir.Substring(srcSubDir.LastIndexOf("\\") + 1));
81 if (!Directory.Exists(targetSubDir))
82 {
83 Directory.CreateDirectory(targetSubDir);
84 MoveFiles(srcSubDir, targetSubDir, overWrite,moveSub);
85 }
86 }
87 }
88 }
89 }
90
91 public static void Copy(string srcDir, string targetDir, bool overWrite, bool copySub)
92 {
93 foreach (string srcFileName in Directory.GetFiles(srcDir))
94 {
95 string targetFileName = Path.Combine(targetDir, srcDir.Substring(srcDir.LastIndexOf("\\") + 1));
96 if (File.Exists(targetFileName))
97 {
98 if (overWrite)
99 {
100 File.SetAttributes(targetFileName, FileAttributes.Normal);
101 File.Copy(srcFileName, targetFileName, overWrite);
102 }
103
104 }
105 else
106 {
107 File.Copy(srcFileName, targetFileName, overWrite);
108 }
109 }
110 if (copySub)
111 {
112 foreach (string srcSubDir in Directory.GetDirectories(srcDir))
113 {
114 string targetSubDir = Path.Combine(targetDir, srcSubDir.Substring(srcSubDir.LastIndexOf("\\") + 1));
115 if (!Directory.Exists(targetSubDir))
116 {
117 Directory.CreateDirectory(targetSubDir);
118 Copy(srcSubDir, targetSubDir, overWrite, true);
119 }
120 }
121 }
122 }
123
124 public static string GetChildFileInfo(string dir)
125 {
126 StringBuilder sb = new StringBuilder();
127 sb.Append("{");
128 try
129 {
130 foreach (string fileName in Directory.GetFiles(dir))
131 {
132 FileInfo fileInfo = new FileInfo(fileName);
133 string name = fileName.Substring(fileName.LastIndexOf("\\") + 1);
134 string addr = fileName;
135 string size = fileInfo.Length.ToString();
136 string createTime = fileInfo.CreationTime.ToString();
137 sb.Append("{name:" + name);
138 sb.Append(",addr:" + addr);
139 sb.Append(",size:" + size);
140 sb.Append(",createTime:" + createTime);
141 sb.Append("},");
142 // generate a tree
143 }
144 foreach (string subDir in Directory.GetDirectories(dir))
145 {
146 GetChildFileInfo(subDir);
147 }
148 }
149 catch(Exception ex)
150 {
151
152 }
153 if (sb.Length > 2)
154 sb.Remove(sb.Length - 1, 1);
155 sb.Append("}");
156 return sb.ToString();
157 }
158 /// <summary>
159 /// HW变形,W按宽度成比例,H按高度成比例,Cut不变形
160 /// </summary>
161 public enum ImageCutMode
162 {
163 HW,// HW变形
164 W, // W按宽度成比例
165 H, // H按高度成比例
166 Cut // Cut不变形
167 }
168
169 public static MemoryStream CutImage(Image oImage, int width, int height, ImageCutMode mode)
170 {
171 Image originalImage = oImage;
172 MemoryStream ms = new MemoryStream();
173
174 int towidth = width;
175 int toheight = height;
176
177 int x = 0;
178 int y = 0;
179 int ow = oImage.Width;
180 int oh = oImage.Height;
181
182 switch (mode)
183 {
184 case ImageCutMode.HW://指定高宽缩放(可能变形)
185 break;
186 case ImageCutMode.W://指定宽,高按比例
187 toheight = oh * width / ow;
188 break;
189 case ImageCutMode.H://指定高,宽按比例
190 towidth = ow * height / oh;
191 break;
192 case ImageCutMode.Cut://指定高宽裁减(不变形)
193 if ((double)ow / (double)oh > (double)towidth / (double)toheight)
194 {
195 ow = oh * towidth / toheight;
196 y = 0;
197 x = (oImage.Width - ow) / 2;
198 }
199 else
200 {
201 oh = ow * height / towidth;
202 x = 0;
203 y = (oImage.Height - oh) / 2;
204 }
205 break;
206 default:
207 break;
208 }
209
210 //新建一个bmp图片
211 Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
212
213 //新建一个画板
214 Graphics g = System.Drawing.Graphics.FromImage(bitmap);
215
216 //设置高质量插值法
217 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
218
219 //设置高质量,低速度呈现平滑程度
220 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
221
222 //清空画布并以透明背景色填充
223 g.Clear(Color.Transparent);
224
225 //在指定位置并且按指定大小绘制原图片的指定部分
226 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
227 new Rectangle(x, y, ow, oh),
228 GraphicsUnit.Pixel);
229
230 try
231 {
232 //以jpg格式保存缩略图
233 bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
234 }
235 catch (System.Exception e)
236 {
237 throw e;
238 }
239 finally
240 {
241 originalImage.Dispose();
242 bitmap.Dispose();
243 g.Dispose();
244 }
245 return ms;
246 }
247
248 public static MemoryStream ZoomImage(byte[] buffer, int width, int height, ImageCutMode mode)
249 {
250 MemoryStream ms=new MemoryStream();
251 ms.Write(buffer,0,buffer.Length);
252 Image oImage = Image.FromStream(ms);
253 MemoryStream outms = CutImage(oImage, width, height, ImageCutMode.HW);
254 return outms;
255 }
256
257 /**/
258 /// <summary>
259 /// 生成缩略图
260 /// </summary>
261 /// <param name="originalImagePath">源图路径(物理路径)</param>
262 /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
263 /// <param name="width">缩略图宽度</param>
264 /// <param name="height">缩略图高度</param>
265 /// <param name="mode">生成缩略图的方式</param>
266 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ImageCutMode mode)
267 {
268 Image originalImage = Image.FromFile(originalImagePath);
269
270 int towidth = width;
271 int toheight = height;
272
273 int x = 0;
274 int y = 0;
275 int ow = originalImage.Width;
276 int oh = originalImage.Height;
277
278 switch (mode)
279 {
280 case ImageCutMode.HW://指定高宽缩放(可能变形)
281 break;
282 case ImageCutMode.W://指定宽,高按比例
283 toheight = originalImage.Height * width / originalImage.Width;
284 break;
285 case ImageCutMode.H://指定高,宽按比例
286 towidth = originalImage.Width * height / originalImage.Height;
287 break;
288 case ImageCutMode.Cut://指定高宽裁减(不变形)
289 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
290 {
291 oh = originalImage.Height;
292 ow = originalImage.Height * towidth / toheight;
293 y = 0;
294 x = (originalImage.Width - ow) / 2;
295 }
296 else
297 {
298 ow = originalImage.Width;
299 oh = originalImage.Width * height / towidth;
300 x = 0;
301 y = (originalImage.Height - oh) / 2;
302 }
303 break;
304 default:
305 break;
306 }
307
308 //新建一个bmp图片
309 Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
310
311 //新建一个画板
312 Graphics g = System.Drawing.Graphics.FromImage(bitmap);
313
314 //设置高质量插值法
315 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
316
317 //设置高质量,低速度呈现平滑程度
318 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
319
320 //清空画布并以透明背景色填充
321 g.Clear(Color.Transparent);
322
323 //在指定位置并且按指定大小绘制原图片的指定部分
324 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
325 new Rectangle(x, y, ow, oh),
326 GraphicsUnit.Pixel);
327
328 try
329 {
330 //以jpg格式保存缩略图
331 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
332 }
333 catch (System.Exception e)
334 {
335 throw e;
336 }
337 finally
338 {
339 originalImage.Dispose();
340 bitmap.Dispose();
341 g.Dispose();
342 }
343 }
344
345 public static MemoryStream WaterPrint(MemoryStream ms, string word, PointF pos)
346 {
347 MemoryStream outms = new MemoryStream();
348 Image img = Image.FromStream(ms);
349 Graphics g = Graphics.FromImage(img);
350 g.DrawImage(img, 0, 0, img.Width, img.Height);
351 Font f = new Font("Verdana", 16);
352 Brush b = new System.Drawing.SolidBrush(Color.Blue);
353 g.DrawString(word, f, b,pos);
354 img.Save(outms, System.Drawing.Imaging.ImageFormat.Jpeg);
355 img.Dispose();
356 return outms;
357 }
358 }
359

 

posted @ 2010-11-04 16:59  pingjiang  阅读(431)  评论(0编辑  收藏  举报