C#压缩图片算法

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
    using System.IO; 
    using System.Drawing; 
    using System.Drawing.Imaging; 
    using System; 
    namespace Bll 
    
        /// <summary> 
        /// 图片处理类 
        /// 1、生成缩略图片或按照比例改变图片的大小和画质 
        /// 2、将生成的缩略图放到指定的目录下 
        /// </summary> 
        public class ImageHepler 
        
            public Image ResourceImage, ReducedImage; 
            private int ImageWidth; 
            private int ImageHeight; 
            public string ErrMessage; 
       
            /// <summary> 
            /// 类的构造函数 
            /// </summary> 
            /// <param name="ImageFileName">图片文件的全路径名称</param> 
            public ImageHepler(string ImageFileName) 
            
                ResourceImage = Image.FromFile(ImageFileName); 
                ErrMessage = ""
            
       
            public bool ThumbnailCallback() 
            
                return false
            
       
            /// <summary> 
            /// 生成缩略图,返回缩略图的Image对象 
            /// </summary> 
            /// <param name="Width">缩略图的宽度</param> 
            /// <param name="Height">缩略图的高度</param> 
            /// <returns>缩略图的Image对象</returns> 
            public Image GetReducedImage(int Width, int Height) 
            
                double LengthLong;          //存储(长和宽中)较短的长度 
                int widthOK, heightOK;      //存储实际要生成的图片的长宽 
                if (Width < Height)         //判断输入的长和宽那个较短 
                
                    LengthLong = Width;     //把较短的存储在 LengthLonh 用于计算 
                
                else 
                
                    LengthLong = Height; 
                
                try 
                
                    //判断原图片 长和宽  
                    //原图比较长的一个边要和缩略图的边相等 
                    if (ResourceImage.Width > ResourceImage.Height) 
                    
                        widthOK = (int)LengthLong; 
                        heightOK = (int)(LengthLong / ResourceImage.Width * ResourceImage.Height); 
                    
                    else 
                    
                        heightOK = (int)LengthLong; 
                        widthOK = (int)LengthLong / ResourceImage.Height * ResourceImage.Width; 
       
                    
                    Image ReducedImage; 
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); 
                    ReducedImage = ResourceImage.GetThumbnailImage(widthOK, heightOK, callb, IntPtr.Zero); 
                    return ReducedImage; 
                
                catch (Exception e) 
                
                    ErrMessage = e.Message; 
                    return null
                
            
       
            /// <summary> 
            /// 生成缩略图,将缩略图文件保存到指定的路径 
            /// </summary> 
            /// <param name="Width">缩略图的宽度</param> 
            /// <param name="Height">缩略图的高度</param> 
            /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param> 
            /// <returns>成功返回true,否则返回false</returns> 
            public bool GetReducedImage(int Width, int Height, string targetFilePath) 
            
                double LengthLong;          //存储(长和宽中)较短的长度 
                int widthOK, heightOK;      //存储实际要生成的图片的长宽 
                if (Width < Height)         //判断输入的长和宽那个较短 
                
                    LengthLong = Width;     //把较短的存储在 LengthLonh 用于计算 
                
                else 
                
                    LengthLong = Height; 
                
                try 
                
                    //判断原图片 长和宽  
                    //原图比较长的一个边要和缩略图的边相等 
                    if (ResourceImage.Width > ResourceImage.Height) 
                    
                        widthOK = (int)LengthLong; 
                        heightOK = (int)(LengthLong / ResourceImage.Width * ResourceImage.Height); 
                    
                    else 
                    
                        heightOK = (int)LengthLong; 
                        widthOK = (int)LengthLong / ResourceImage.Height * ResourceImage.Width; 
                    
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); 
                    ReducedImage = ResourceImage.GetThumbnailImage(widthOK, heightOK, callb, IntPtr.Zero); 
                    ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg); 
                    //ReducedImage.Dispose(); 
                    return true
                
                catch (Exception e) 
                
                    ErrMessage = e.Message; 
                    return false
                
            
       
            /// <summary> 
            /// 生成缩略图,返回缩略图的Image对象 
            /// </summary> 
            /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>   
            /// <returns>缩略图的Image对象</returns> 
            public Image GetReducedImage(double Percent) 
            
                try 
                
                    Image ReducedImage; 
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); 
                    ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent); 
                    ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent); 
                    ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero); 
                    return ReducedImage; 
                
                catch (Exception e) 
                
                    ErrMessage = e.Message; 
                    return null
                
            
       
            /// <summary> 
            /// 生成缩略图,返回缩略图的Image对象 
            /// </summary> 
            /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>   
            /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param> 
            /// <returns>成功返回true,否则返回false</returns> 
            public bool GetReducedImage(double Percent, string targetFilePath) 
            
                try 
                
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback); 
                    ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent); 
                    ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent); 
                    ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero); 
                    ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg); 
                    //ReducedImage.Dispose(); 
                    return true
                
                catch (Exception e) 
                
                    ErrMessage = e.Message; 
                    return false
                
            
        
    
 
 
[csharp] view plaincopy
 
    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Linq; 
    using System.Web; 
    using System.IO; 
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Security.AccessControl; 
    using System.Security.Permissions; 
    namespace Bll 
    
        public class FolderHelper 
        
            //判断文件夹是否存在 
            public static bool checkFolderExits(string path) 
            
                DirectoryInfo dir = new DirectoryInfo(path); 
                if (dir.Exists)//文件夹存在 
                {    
                    return true
                
                else 
                
                   //dir.Create();//不存在就创建一个 
                    return false
                
            
            //创建一个文件夹,存在就创建失败 
            public static bool CreateNewFolder(string path) 
            
                DirectoryInfo dir = new DirectoryInfo(path); 
       
                if (!dir.Exists) 
                
                    dir.Create(); 
                    return true
                
                else 
                    return false
            
            /// <summary> 
            /// 在指定目录下创建指定名称文件夹 
            /// </summary> 
            /// <param name="ParentsPath"></param> 
            /// <param name="NewFolderName"></param> 
            /// <returns></returns> 
            public static bool CreateNewFolder(string ParentsPath, string NewFolderName) 
            
                string CreatePath = ParentsPath + @"\" + NewFolderName; 
                DirectoryInfo dir = new DirectoryInfo(CreatePath); 
       
                if (!dir.Exists) 
                
                    dir.Create(); 
                    return true
                
                else 
                    return false
            
            /// <summary> 
            /// 返回目录下的所有文件名 
            /// </summary> 
            /// <param name="path"></param> 
            /// <returns></returns> 
            public static ArrayList getAllFiles(string path) 
            
                DirectoryInfo dir = new DirectoryInfo(path); 
                if (dir.Exists) 
                
                    FileInfo[] fileinfo = dir.GetFiles(); 
                    ArrayList list = new ArrayList(); 
                    foreach (FileInfo f in fileinfo) 
                    
                        list.Add(f.Name); 
                    
                    return list; 
                
                else 
                    return null
            
            /// <summary> 
            /// 计算文件夹的大小 
            /// </summary> 
            /// <param name="d"></param> 
            /// <returns></returns> 
            public static long DirSize(DirectoryInfo d) 
            
                long Size = 0; 
                // Add file sizes. 
                FileInfo[] fis = d.GetFiles();//获得目录文件列表 
                foreach (FileInfo fi in fis) 
                
                    Size += fi.Length; 
                
                // Add subdirectory sizes. 
                DirectoryInfo[] dis = d.GetDirectories();//获取目录子目录列表 
                foreach (DirectoryInfo di in dis) 
                
                    Size += DirSize(di); 
                
                return Size; 
            
            /// <summary> 
            /// 把文件夹得大小转换成比较合适的表示单位 
            /// </summary> 
            /// <param name="size"></param> 
            /// <returns></returns> 
            public static string ViewSize(long size) 
            
                long m=size; 
                string viewstr; 
                   
                if ((m / 1024) > 0)//表示可以转换成KB 
                
                    m = m / 1024;//转换成KB 
                       
                    if ((m / 1024) > 0)//表示可以转换成MB 
                    
                        m = m / 1024;//转换成MB了 
       
                        if ((m / 1024) > 0)//表示可以转换成GB 
                        
                            m = m / 1024;//转换成GB了 
                            viewstr = m.ToString() + "GB"
                        
                        else 
                        
                            viewstr = m.ToString() + "MB"
                        
                    
                    else 
                    
                        viewstr = m.ToString() + "KB"
                    
                
                else 
                
                    viewstr = m.ToString() + "byte"
                
                return viewstr; 
            
            /// <summary> 
            /// 删除指定目录和内容 
            /// </summary> 
            /// <param name="dir"></param> 
            /// <returns></returns> 
            public static bool delDir(string dir) 
            
                bool flag = false
                DirectoryInfo d = new DirectoryInfo(dir); 
                if (d.Exists)//判断目录是否存在 
                
                    try 
                    
                        d.Delete(); 
                        flag = true
                    
                    catch (Exception e) { flag = false; } 
                
                return flag; 
            
            /// <summary> 
            /// 删除指定文件 
            /// </summary> 
            /// <param name="fil"></param> 
            /// <returns></returns> 
            public static bool delFile(string fil) 
            
                bool flag = false
                FileInfo d = new FileInfo(fil); 
                if (d.Exists)//判断目录是否存在 
                
                    try 
                    
                        d.Delete(); 
                        flag = true
                    
                    catch (Exception e) { flag = false; } 
                
                return flag; 
            
            public static void Copy(string sourceDirectory, string targetDirectory) 
            
                DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); 
                DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); 
       
                CopyAll(diSource, diTarget); 
            
            /// <summary> 
            /// 复制目录及子文件到指定目录 
            /// </summary> 
            /// <param name="source"></param> 
            /// <param name="target"></param> 
            public static void CopyAll(DirectoryInfo source, DirectoryInfo target) 
            
                // Check if the target directory exists, if not, create it. 
                if (Directory.Exists(target.FullName) == false
                
                    Directory.CreateDirectory(target.FullName); 
                
       
                // Copy each file into it's new directory. 
                foreach (FileInfo fi in source.GetFiles()) 
                
                    Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name); 
                    fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); 
                
       
                // Copy each subdirectory using recursion. 
                foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) 
                
                    DirectoryInfo nextTargetSubDir = 
                        target.CreateSubdirectory(diSourceSubDir.Name); 
                    CopyAll(diSourceSubDir, nextTargetSubDir); 
                
            
       
       
       
            /// <summary> 
            /// 循环读取某个目录下的所有文件和目录,查询有关每一项的一些信息。返回一个文件列表 
            /// </summary> 
            /// <param name="strCurrentDir"></param> 
            public static List<fileEntity> FileView(string strCurrentDir) 
            
                List<fileEntity> fileList = new List<fileEntity>(); 
                DirectoryInfo dir = new DirectoryInfo(strCurrentDir); 
       
                foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())//这个循环再读取文件的信息 
                
                    try 
                    
                        //FileSystemInfo 对象可以表示文件或目录,从而可以作为 FileInfo 或 DirectoryInfo 对象的基础。 当分析许多文件和目录时,请使用该基类。 
                        FileInfo fi; 
                        DirectoryInfo di; 
                        //创建一个自己写的实体类的实体 
                        fileEntity newfile = new fileEntity(); 
                        if (fsi is FileInfo)//外层循环读取文件信息 
                        
                            //表示当前fsi是文件 
                            fi = (FileInfo)fsi; 
                            newfile.fileName = fi.Name; 
                            newfile.fileExt = fi.Extension; 
                            newfile.fileSize = fi.Length; 
                            newfile.FileModify = fi.LastWriteTime; 
                            //通过扩展名来选择文件显示图标 
                            switch (newfile.fileExt) 
                            
                                case ".gif"
                                    newfile.picName = "gif.gif"
                                    break
                                default
                                    newfile.picName = "other.gif"
                                    break
                            
                            newfile.picName = "<img src='" + newfile.picName + "' width=25 height=20>"
                        
                        else 
                        
                            di = (DirectoryInfo)fsi; 
                            newfile.fileName = di.Name; 
                            newfile.fileSize = DirSize(di);//调用计算文件夹大小的方法 
                            newfile.FileModify = di.LastWriteTime; 
                            newfile.picName = "<img src='directory.gif' width=25 height=20>"
                        
                        fileList.Add(newfile); 
                    
                    catch (Exception e) { } 
                
                return fileList; 
       
            
       
       
            /// <summary> 
            /// 显示目录和文件 
            /// </summary> 
            /// <param name="path"></param> 
            public static void All(string path) 
            
                FileInfo fi;//文件 
                DirectoryInfo di;//目录 
                DirectoryInfo dir=null
                int i = 0; //控制行的颜色 
                try 
                
                    dir = new DirectoryInfo(path); 
                
                catch (Exception e) { } 
                foreach (FileSystemInfo fsi in dir.GetFileSystemInfos()) 
                
                    try 
                    
                        fileEntity newfile = new fileEntity(); 
                        FolderEntity folder = new FolderEntity(); 
                        newfile.fileName = ""
                        newfile.picName = ""
                        newfile.fileExt = ""
                        newfile.fileSize = 0; 
                        folder.folderName = ""
                        folder.picName = ""
       
                        i += 1; 
                        if (fsi is FileInfo)//判断当前读取的是不是一个文件 
                        
                            //表示当前fsi是文件 
                            fi = (FileInfo)fsi; 
                            newfile.fileName = fi.Name; 
                            newfile.fileExt = fi.Extension; 
                            newfile.fileSize = fi.Length; 
                            newfile.FileModify = fi.LastWriteTime; 
       
                            //将文件加上可以下载文件的链接 
       
       
                            newfile.fileName = "<a href='........'></a>"
       
       
                            //通过扩展名来选择文件显示图标 
       
                            //Response.Write(Session["DataBasePath"].ToString()+"\\filetype\\"+pfun.getFileExt(FileExt)+".gif"); 
       
                            if (fsi.Exists) 
                            
                                switch (newfile.fileExt) 
                                
                                    case ".gif"
                                        newfile.picName = "gif.gif"
                                        break
                                    default
                                        newfile.picName = "other.gif"
                                        break
                                
                            
                            else 
                            
                                newfile.picName = "unknown.gif"
                            
       
       
                            /*
                            switch(FileExt)
                            {
                                case ".gif":
                                    FilePic = "gif.gif";
                                    break;
                                default:
                                    FilePic = "other.gif";
                                    break;
                            }
                            */ 
       
                            newfile.picName = "<img src='filetype/" + newfile.picName + "' width=16 height=16>"
       
                        
                        else 
                        
                            //当前为目录 
                            di = (DirectoryInfo)fsi; 
                            folder.folderName = di.Name; 
       
                            //给目录加上链接 
       
                            folder.folderName = "<a href='.......'><a>"
                            folder.lastTime = di.LastWriteTime; 
                            folder.picName = "<img src='filetype/folder.gif' width=16 height=16>"
       
                        
                    }catch(Exception e){} 
                
       
       
            
        
    }