ImageAlgorithmTools.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //图像处理工具
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Media.Imaging;
  11. namespace MvvmScaffoldFrame48.DLL.ImageAlgorithm
  12. {
  13. public static class ImageAlgorithmTools
  14. {
  15. /// <summary>
  16. /// 根据ROI裁剪图像
  17. /// </summary>
  18. /// <param name="sourceBitmap"></param>
  19. /// <param name="roi"></param>
  20. /// <returns></returns>
  21. /// <exception cref="ArgumentException"></exception>
  22. public static Bitmap CropBitmap(Bitmap sourceBitmap, Rectangle roi)
  23. {
  24. // 检查边界
  25. if (roi.X < 0 || roi.Y < 0 ||
  26. roi.Right > sourceBitmap.Width ||
  27. roi.Bottom > sourceBitmap.Height)
  28. {
  29. throw new ArgumentException("ROI区域超出源图像边界");
  30. }
  31. // 创建目标位图
  32. Bitmap result = new Bitmap(roi.Width, roi.Height, sourceBitmap.PixelFormat);
  33. // 锁定位图数据
  34. BitmapData sourceData = sourceBitmap.LockBits(
  35. new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height),
  36. ImageLockMode.ReadOnly,
  37. sourceBitmap.PixelFormat);
  38. BitmapData resultData = result.LockBits(
  39. new Rectangle(0, 0, result.Width, result.Height),
  40. ImageLockMode.WriteOnly,
  41. result.PixelFormat);
  42. try
  43. {
  44. unsafe
  45. {
  46. byte* sourcePtr = (byte*)sourceData.Scan0;
  47. byte* resultPtr = (byte*)resultData.Scan0;
  48. int pixelSize = Image.GetPixelFormatSize(sourceBitmap.PixelFormat) / 8;
  49. int sourceStride = sourceData.Stride;
  50. int resultStride = resultData.Stride;
  51. for (int y = 0; y < roi.Height; y++)
  52. {
  53. byte* srcRow = sourcePtr + (roi.Y + y) * sourceStride + roi.X * pixelSize;
  54. byte* dstRow = resultPtr + y * resultStride;
  55. // 复制一行像素数据
  56. for (int x = 0; x < roi.Width * pixelSize; x++)
  57. {
  58. dstRow[x] = srcRow[x];
  59. }
  60. }
  61. }
  62. }
  63. finally
  64. {
  65. sourceBitmap.UnlockBits(sourceData);
  66. result.UnlockBits(resultData);
  67. }
  68. return result;
  69. }
  70. /// <summary>
  71. /// Bitmap 转 BitmapImage 的辅助方法
  72. /// </summary>
  73. /// <param name="bitmap"></param>
  74. /// <returns></returns>
  75. public static BitmapImage ConvertToBitmapImage(Bitmap bitmap)
  76. {
  77. using (MemoryStream memory = new MemoryStream())
  78. {
  79. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  80. memory.Position = 0;
  81. var bitmapImage = new BitmapImage();
  82. bitmapImage.BeginInit();
  83. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  84. bitmapImage.DecodePixelWidth = 1440; // 设置目标分辨率宽度
  85. bitmapImage.DecodePixelHeight = 1080; // 保持宽高比
  86. bitmapImage.StreamSource = memory;
  87. bitmapImage.EndInit();
  88. return bitmapImage;
  89. }
  90. }
  91. }
  92. }