ImageCanvas.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO.Ports;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using System.Windows.Threading;
  11. namespace IDInfoExtraction
  12. {
  13. public class RoiInfo
  14. {
  15. public Rect RoiRect { get; set; }
  16. public System.Windows.Media.Brush Color { get; set; }
  17. }
  18. public class MyTransform
  19. {
  20. public double Scale { get; set; }
  21. public double OffsetX { get; set; }
  22. public double OffsetY { get; set; }
  23. public MyTransform(double scale, double offsetX, double offsetY)
  24. {
  25. Scale = scale;
  26. OffsetX = offsetX;
  27. OffsetY = offsetY;
  28. }
  29. public System.Windows.Point Transform(System.Windows.Point point)
  30. {
  31. double x = OffsetX + Scale * point.X;
  32. double y = OffsetY + Scale * point.Y;
  33. return new System.Windows.Point(x, y);
  34. }
  35. public System.Windows.Rect Transform(System.Windows.Rect rect)
  36. {
  37. double left = OffsetX + Scale * rect.Left;
  38. double top = OffsetY + Scale * rect.Top;
  39. double width = Scale * rect.Width;
  40. double height = Scale * rect.Height;
  41. return new System.Windows.Rect(left, top, width, height);
  42. }
  43. }
  44. /// <summary>
  45. /// ImageCanvas.xaml 的交互逻辑
  46. /// </summary>
  47. public partial class ImageCanvas : UserControl
  48. {
  49. #region private variables
  50. private Bitmap _imageRGB;
  51. private MyTransform _transformRGB;
  52. #endregion private variables
  53. #region 用户界面响应
  54. private void CanvasSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
  55. {
  56. UpdateRGBTransforms();
  57. }
  58. #endregion 用户界面响应
  59. #region public funcs
  60. public ImageCanvas()
  61. {
  62. InitializeComponent();
  63. }
  64. public void UpdateRGBImage(Bitmap image)
  65. {
  66. if (_imageRGB != null)
  67. {
  68. _imageRGB.Dispose();
  69. }
  70. _imageRGB = image.Clone(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), image.PixelFormat);
  71. // 更新缩放比例
  72. UpdateRGBTransforms();
  73. // 更新图像显示
  74. var bitmapImage = BitmapToBitmapImage(image);
  75. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  76. {
  77. //RGBImageShow.Opacity = 0.3; // 透明化
  78. RGBImageShow.Source = bitmapImage;
  79. }));
  80. }
  81. public Bitmap GetRGBImageAsBitmap()
  82. {
  83. return _imageRGB.Clone(new System.Drawing.Rectangle(0, 0, _imageRGB.Width, _imageRGB.Height), _imageRGB.PixelFormat);
  84. }
  85. #endregion public funcs
  86. #region private funcs
  87. private BitmapImage BitmapToBitmapImage(Bitmap img)
  88. {
  89. BitmapImage bmpimg = new BitmapImage();
  90. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  91. {
  92. img.Save(ms, ImageFormat.Png);
  93. bmpimg.BeginInit();
  94. bmpimg.StreamSource = ms;
  95. bmpimg.CacheOption = BitmapCacheOption.OnLoad;
  96. bmpimg.EndInit();
  97. bmpimg.Freeze();
  98. ms.Dispose();
  99. }
  100. return bmpimg;
  101. }
  102. private void UpdateRGBTransforms()
  103. {
  104. if (_imageRGB != null)
  105. {
  106. // 使图片保持长宽比例,居中显示,需要平移和缩放
  107. var imgWidth = _imageRGB.Width;
  108. var imgHeight = _imageRGB.Height;
  109. var canvasWidth = RGBImageCanvas.ActualWidth;
  110. var canvasHeight = RGBImageCanvas.ActualHeight;
  111. var scaleX = canvasWidth / imgWidth;
  112. var scaleY = canvasHeight / imgHeight;
  113. var scale = scaleX < scaleY ? scaleX : scaleY;
  114. double offsetX, offsetY;
  115. if (Math.Abs(scale - scaleX) < 0.0001)
  116. {
  117. offsetY = 0.5 * (scaleY - scale) * imgHeight;
  118. offsetX = 0;
  119. }
  120. else
  121. {
  122. offsetX = 0.5 * (scaleX - scale) * imgWidth;
  123. offsetY = 0;
  124. }
  125. _transformRGB = new MyTransform(scale, offsetX, offsetY);
  126. }
  127. }
  128. public void UpdateRoughPathResults(List<System.Drawing.Point> pathPts, string name)
  129. {
  130. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  131. {
  132. DrawContoursInCanvas(pathPts, name, System.Windows.Media.Brushes.Cyan);
  133. }));
  134. }
  135. public void ClearCanvas()
  136. {
  137. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  138. {
  139. // 清空画布上的所有内容
  140. RGBImageCanvas.Children.Clear();
  141. }));
  142. }
  143. /// <summary>
  144. /// 在画布上画出轮廓
  145. /// </summary>
  146. /// <param name="pts"></param>
  147. /// <param name="registerName"></param>
  148. private void DrawContoursInCanvas(List<System.Drawing.Point> pts, string registerName, System.Windows.Media.Brush color)
  149. {
  150. if (_transformRGB == null)
  151. {
  152. return;
  153. }
  154. // 已有则删除
  155. RemoveContoursInCanvas(registerName);
  156. if (pts == null)
  157. {
  158. return;
  159. }
  160. System.Windows.Media.PointCollection contourPoints = new PointCollection();
  161. foreach (var pt in pts)
  162. {
  163. var point = new System.Windows.Point(pt.X, pt.Y);
  164. var transPoint = _transformRGB.Transform(point);
  165. contourPoints.Add(transPoint);
  166. }
  167. // 添加第一个点 闭合矩形框
  168. var point0 = new System.Windows.Point(pts[0].X, pts[0].Y);
  169. var transPoint0 = _transformRGB.Transform(point0);
  170. contourPoints.Add(transPoint0);
  171. System.Windows.Shapes.Polyline contourDraw = new System.Windows.Shapes.Polyline();
  172. contourDraw.Points = contourPoints;
  173. contourDraw.StrokeThickness = 3;
  174. contourDraw.Stroke = color;
  175. RGBImageCanvas.Children.Add(contourDraw);
  176. RGBImageCanvas.RegisterName(registerName, contourDraw);
  177. }
  178. /// <summary>
  179. /// 从画布上将轮廓移除
  180. /// </summary>
  181. /// <param name="registerName"></param>
  182. public void RemoveContoursInCanvas(string registerName)
  183. {
  184. System.Windows.Shapes.Polyline contourExist = RGBImageCanvas.FindName(registerName) as System.Windows.Shapes.Polyline;
  185. if (contourExist != null)
  186. {
  187. RGBImageCanvas.Children.Remove(contourExist);
  188. RGBImageCanvas.UnregisterName(registerName);
  189. }
  190. }
  191. public void UpdateCamBox()
  192. {
  193. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  194. {
  195. // 清空画布上的所有内容
  196. RGBImageCanvas.Children.Clear();
  197. Rect[] rcst = new Rect[]
  198. {
  199. new Rect(70, 70, 500, 300)
  200. };
  201. DrawRectsInCanvas(rcst, "box");
  202. }));
  203. }
  204. /// <summary>
  205. /// 在画布上画出矩形框
  206. /// </summary>
  207. /// <param name="rcs"></param>
  208. /// <param name="rcName"></param>
  209. private void DrawRectsInCanvas(Rect[] rcs, string rcName)
  210. {
  211. if (_transformRGB == null)
  212. {
  213. return;
  214. }
  215. // 已有则删除
  216. RemoveRectsCanvas(rcName);
  217. List<System.Windows.Shapes.Rectangle> rcRenders = new List<System.Windows.Shapes.Rectangle> { };
  218. foreach (var rc in rcs)
  219. {
  220. System.Windows.Shapes.Rectangle rcRender = new System.Windows.Shapes.Rectangle();
  221. rcRender.StrokeThickness = 2;
  222. rcRender.Stroke = System.Windows.Media.Brushes.AliceBlue;
  223. var transPtLeftTop = _transformRGB.Transform(new System.Windows.Point(rc.Left, rc.Top));
  224. var transPtRightDown = _transformRGB.Transform(new System.Windows.Point(rc.Left + rc.Width, rc.Top + rc.Height));
  225. Canvas.SetLeft(rcRender, transPtLeftTop.X);
  226. Canvas.SetTop(rcRender, transPtLeftTop.Y);
  227. rcRender.Width = transPtRightDown.X - transPtLeftTop.X;
  228. rcRender.Height = transPtRightDown.Y - transPtLeftTop.Y;
  229. RGBImageCanvas.Children.Add(rcRender);
  230. //RGBImageCanvas.RegisterName(rcName, rcRender);
  231. rcRenders.Add(rcRender);
  232. }
  233. RGBImageCanvas.RegisterName(rcName, rcRenders);
  234. }
  235. /// <summary>
  236. /// 从画布上移除矩形框
  237. /// </summary>
  238. /// <param name="roiName"></param>
  239. private void RemoveRectsCanvas(string roiName)
  240. {
  241. // 框
  242. string textName = roiName;
  243. List<System.Windows.Shapes.Rectangle> rcsExist = RGBImageCanvas.FindName(textName) as List<System.Windows.Shapes.Rectangle>;
  244. if (rcsExist != null)
  245. {
  246. foreach (var rcExist in rcsExist)
  247. {
  248. RGBImageCanvas.Children.Remove(rcExist);
  249. }
  250. RGBImageCanvas.UnregisterName(textName);
  251. }
  252. }
  253. private Bitmap CropImage(Bitmap source, Rect cropRect)
  254. {
  255. // 创建一个与矩形大小相同的Bitmap
  256. Bitmap croppedImage = new Bitmap((int)cropRect.Width, (int)cropRect.Height);
  257. // 创建一个Graphics对象,并从原始图像中裁剪指定区域的图像
  258. using (Graphics g = Graphics.FromImage(croppedImage))
  259. {
  260. g.DrawImage(source, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height),
  261. (int)cropRect.Left, (int)cropRect.Top, croppedImage.Width, croppedImage.Height,
  262. GraphicsUnit.Pixel);
  263. }
  264. return croppedImage;
  265. }
  266. #endregion private funcs
  267. }
  268. }