ImageAlgorithmTools.cs 3.5 KB

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