LoadSplieImageClass.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace CCDCount.DLL
  10. {
  11. public class LoadSplieImageClass
  12. {
  13. #region 变量
  14. //读取的图片路径
  15. private string SplieImagePath = "";
  16. //读取到的图像
  17. Bitmap bitmap;
  18. #endregion
  19. #region 公共方法
  20. public void LoadImage(string ImagePath)
  21. {
  22. SplieImagePath = ImagePath;
  23. bitmap = new Bitmap(ImagePath);
  24. }
  25. /// <summary>
  26. /// 读取图像像素信息并分割
  27. /// 目前仅分解海康VM平台保存的二值化BMP文件可正常读取像素信息
  28. /// </summary>
  29. /// <returns></returns>
  30. public List<byte[]> SplieImage()
  31. {
  32. //初始化返回列表数组
  33. List<byte[]> result = new List<byte[]>();
  34. for (int i = 0; i < bitmap.Height; i++)
  35. {
  36. result.Add(new byte[bitmap.Width]);
  37. }
  38. //读取全部像素信息
  39. BitmapData data = bitmap.LockBits(
  40. new Rectangle(0, 0, bitmap.Width, bitmap.Height),
  41. ImageLockMode.ReadOnly,
  42. bitmap.PixelFormat
  43. );
  44. //分行读取
  45. for (int i = 0; i < bitmap.Height; i++)
  46. {
  47. Marshal.Copy(IntPtr.Add(data.Scan0, i * bitmap.Width * sizeof(byte)), result[i], 0, bitmap.Width);
  48. }
  49. bitmap.UnlockBits(data);
  50. return result; // 原始字节数据(需根据 PixelFormat 解析)
  51. }
  52. #endregion
  53. }
  54. }