1
0

ImageCanvas.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using AI.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. using System.Windows.Threading;
  10. using Rect = AI.Common.Rect;
  11. namespace CameraImageCaptureTest
  12. {
  13. public class RoiInfo
  14. {
  15. public Rect RoiRect { get; set; }
  16. public System.Windows.Media.Brush Color { get; set; }
  17. public RoiInfo(Rect roi, System.Windows.Media.Brush color)
  18. {
  19. RoiRect = roi;
  20. Color = color;
  21. }
  22. }
  23. public class OrganInfo
  24. {
  25. public string OrganName { get; set; }
  26. public Rect BoundingBox { get; set; }
  27. public Point2D[][] Contours { get; set; }
  28. public System.Windows.Media.Brush Color => System.Windows.Media.Brushes.Green;
  29. public OrganInfo(string organName, Rect boundingBox, Point2D[][] contours)
  30. {
  31. OrganName = organName;
  32. BoundingBox = boundingBox;
  33. Contours = contours;
  34. }
  35. }
  36. public class MyTransform
  37. {
  38. public double Scale { get; set; }
  39. public double OffsetX { get; set; }
  40. public double OffsetY { get; set; }
  41. public MyTransform(double scale, double offsetX, double offsetY)
  42. {
  43. Scale = scale;
  44. OffsetX = offsetX;
  45. OffsetY = offsetY;
  46. }
  47. public System.Windows.Point Transform(System.Windows.Point point)
  48. {
  49. double x = OffsetX + Scale * point.X;
  50. double y = OffsetY + Scale * point.Y;
  51. return new System.Windows.Point(x, y);
  52. }
  53. public System.Windows.Rect Transform(System.Windows.Rect rect)
  54. {
  55. double left = OffsetX + Scale * rect.Left;
  56. double top = OffsetY + Scale * rect.Top;
  57. double width = Scale * rect.Width;
  58. double height = Scale * rect.Height;
  59. return new System.Windows.Rect(left, top, width, height);
  60. }
  61. }
  62. /// <summary>
  63. /// ImageCanvas.xaml 的交互逻辑
  64. /// </summary>
  65. public partial class ImageCanvas : UserControl
  66. {
  67. #region private variables
  68. private Bitmap _imageRGB;
  69. private MyTransform _transformRGB;
  70. #endregion
  71. #region 用户界面响应
  72. private void CanvasSizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
  73. {
  74. UpdateRGBTransforms();
  75. }
  76. #endregion
  77. #region public funcs
  78. public ImageCanvas()
  79. {
  80. InitializeComponent();
  81. }
  82. public void UpdateRGBImage(Bitmap image)
  83. {
  84. if (_imageRGB != null)
  85. {
  86. _imageRGB.Dispose();
  87. }
  88. _imageRGB = image.Clone(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), image.PixelFormat);
  89. // 更新缩放比例
  90. UpdateRGBTransforms();
  91. // 更新图像显示
  92. var bitmapImage = BitmapToBitmapImage(image);
  93. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  94. {
  95. RGBImageShow.Source = bitmapImage;
  96. }));
  97. }
  98. #endregion
  99. #region private funcs
  100. private BitmapImage BitmapToBitmapImage(Bitmap img)
  101. {
  102. BitmapImage bmpimg = new BitmapImage();
  103. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  104. {
  105. img.Save(ms, ImageFormat.Png);
  106. bmpimg.BeginInit();
  107. bmpimg.StreamSource = ms;
  108. bmpimg.CacheOption = BitmapCacheOption.OnLoad;
  109. bmpimg.EndInit();
  110. bmpimg.Freeze();
  111. ms.Dispose();
  112. }
  113. return bmpimg;
  114. }
  115. private void UpdateRGBTransforms()
  116. {
  117. if (_imageRGB != null)
  118. {
  119. // 使图片保持长宽比例,居中显示,需要平移和缩放
  120. var imgWidth = _imageRGB.Width;
  121. var imgHeight = _imageRGB.Height;
  122. var canvasWidth = RGBImageCanvas.ActualWidth;
  123. var canvasHeight = RGBImageCanvas.ActualHeight;
  124. var scaleX = canvasWidth / imgWidth;
  125. var scaleY = canvasHeight / imgHeight;
  126. var scale = scaleX < scaleY ? scaleX : scaleY;
  127. double offsetX, offsetY;
  128. if (Math.Abs(scale - scaleX) < 0.0001)
  129. {
  130. offsetY = 0.5 * (scaleY - scale) * imgHeight;
  131. offsetX = 0;
  132. }
  133. else
  134. {
  135. offsetX = 0.5 * (scaleX - scale) * imgWidth;
  136. offsetY = 0;
  137. }
  138. _transformRGB = new MyTransform(scale, offsetX, offsetY);
  139. }
  140. }
  141. #endregion
  142. }
  143. }