ImageProcessor.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OTSIncAReportApp._1_UI.OTSReportExport.DataIntegration
  10. {
  11. class ImageProcessor
  12. {
  13. /// <summary>
  14. /// 颗粒图片缩放到固定大小
  15. /// </summary>
  16. /// <param name="sourceFile"></param>
  17. /// <param name="targetWidth"></param>
  18. /// <param name="targetHeight"></param>
  19. /// <returns></returns>
  20. public Bitmap ResizeImageWithPadding(Bitmap sourceFile, int targetWidth, int targetHeight, Color color)
  21. {
  22. var originalImage = sourceFile;
  23. // 计算缩放后的尺寸,保持宽高比
  24. int scaledWidth, scaledHeight;
  25. if (originalImage.Width > originalImage.Height)
  26. {
  27. scaledWidth = targetWidth;
  28. scaledHeight = (int)(targetWidth * ((double)originalImage.Height / originalImage.Width));
  29. }
  30. else
  31. {
  32. scaledHeight = targetHeight;
  33. scaledWidth = (int)(targetHeight * ((double)originalImage.Width / originalImage.Height));
  34. }
  35. // 创建新的Bitmap,大小为目标尺寸
  36. Bitmap newImage = new Bitmap(targetWidth, targetHeight);
  37. Graphics graphic = Graphics.FromImage(newImage);
  38. // 填充整个图像为黑色
  39. graphic.Clear(color);
  40. // 设置高质量插值法
  41. //graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  42. // 计算图片在新图像中的位置(居中)
  43. int x = (targetWidth - scaledWidth) / 2;
  44. int y = (targetHeight - scaledHeight) / 2;
  45. // 绘制缩放后的图片
  46. graphic.DrawImage(originalImage, new Rectangle(x, y, scaledWidth, scaledHeight));
  47. // 保存新图像
  48. //newImage.Save(destFile, ImageFormat.Png); // 或使用ImageFormat.Jpeg等
  49. return newImage;
  50. }
  51. /// <summary>
  52. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  53. /// </summary>
  54. /// <param name="in_cotsparticleclr"></param>
  55. /// <returns></returns>
  56. public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect)
  57. {
  58. //为了能把整个颗粒显示完整
  59. offset_rect.X = offset_rect.X - 5;
  60. offset_rect.Y = offset_rect.Y - 5;
  61. offset_rect.Width = offset_rect.Width + 10;
  62. offset_rect.Height = offset_rect.Height + 10;
  63. //防止计算偏差后,有坐标溢出现象
  64. if (offset_rect.X < 0)
  65. offset_rect.X = 0;
  66. if (offset_rect.Y < 0)
  67. offset_rect.Y = 0;
  68. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  69. {
  70. offset_rect.Width = ls_bt.Width - offset_rect.X;
  71. }
  72. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  73. {
  74. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  75. }
  76. Bitmap new_ret_bp;
  77. //防止为0后面计算出错
  78. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  79. {
  80. //最后通过list_showsegment组建成新的图片,进行返回
  81. new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat);
  82. }
  83. else
  84. {
  85. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  86. }
  87. return new_ret_bp;
  88. }
  89. public Bitmap GetReZoomBitmap(Bitmap in_bp)
  90. {
  91. Mat src = OpenCvSharp.Extensions.BitmapConverter.ToMat(in_bp);
  92. Mat dst = new Mat();
  93. Cv2.AdaptiveThreshold(src, dst, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 27, 25);
  94. //绝对缩放,
  95. Mat dst2 = new Mat();
  96. int col = dst.Width;//获取原图像的大小
  97. int rows = dst.Height;
  98. Cv2.Resize(dst, dst2, new OpenCvSharp.Size(4 * col, 4 * rows), 0, 0, InterpolationFlags.Cubic);
  99. return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(dst2);
  100. }
  101. /// <summary>
  102. /// Resize图片
  103. /// </summary>
  104. /// <param name="bmp">原始Bitmap</param>
  105. /// <param name="newW">新的宽度</param>
  106. /// <param name="newH">新的高度</param>
  107. /// <returns>处理以后的图片</returns>
  108. public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
  109. {
  110. try
  111. {
  112. Bitmap b = new Bitmap(newW, newH);
  113. Graphics g = Graphics.FromImage(b);
  114.                 // 插值算法的质量
  115.                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  116. g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  117. g.Dispose();
  118. return b;
  119. }
  120. catch
  121. {
  122. return null;
  123. }
  124. }
  125. }
  126. }