PathGetPmgImageClass.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Extensometer.WinForm
  10. {
  11. public class PathGetPmgImageClass
  12. {
  13. private static List<Bitmap> Bitmaps = new List<Bitmap>();
  14. //private static List<FileInfo> GetFile = new List<FileInfo>();
  15. public List<Bitmap> GetBitmaps(string Path)
  16. {
  17. getdir(Path);
  18. return Bitmaps;
  19. }
  20. private static void getdir(string Path)
  21. {
  22. try
  23. {
  24. string extName = ".bmp";
  25. string[] dir = Directory.GetDirectories(Path); //文件夹列表
  26. DirectoryInfo fdir = new DirectoryInfo(Path);
  27. FileInfo[] file = fdir.GetFiles();
  28. //FileInfo[] file = Directory.GetFiles(path); //文件列表
  29. if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
  30. {
  31. foreach (FileInfo f in file) //显示当前目录所有文件
  32. {
  33. if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
  34. {
  35. //GetFile.Add(f);
  36. Bitmaps.Add(new Bitmap(Image.FromFile(f.FullName)));
  37. }
  38. }
  39. foreach (string d in dir)
  40. {
  41. getdir(d);//递归
  42. }
  43. }
  44. }
  45. catch
  46. {
  47. }
  48. }
  49. private Bitmap BytesToBitmap(byte[] Bytes)
  50. {
  51. MemoryStream stream = null;
  52. try
  53. {
  54. stream = new MemoryStream(Bytes);
  55. return new System.Drawing.Bitmap((System.Drawing.Image)new System.Drawing.Bitmap(stream));
  56. }
  57. catch (ArgumentNullException ex)
  58. {
  59. throw ex;
  60. }
  61. catch (ArgumentException ex)
  62. {
  63. throw ex;
  64. }
  65. finally
  66. {
  67. stream.Close();
  68. }
  69. }
  70. private byte[] BitmapToBytes(Bitmap bitmap)
  71. {
  72. byte[] _imgData = null;
  73. using (MemoryStream ms = new MemoryStream())
  74. {
  75. bitmap.Save(ms, ImageFormat.Bmp);
  76. _imgData = new byte[ms.Length];
  77. ms.Seek(0, SeekOrigin.Begin);
  78. ms.Read(_imgData, 0, Convert.ToInt32(ms.Length));
  79. }
  80. return _imgData;
  81. }
  82. }
  83. }