HWindowTool_Smart.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using HalconDotNet;
  10. using System.Diagnostics;
  11. namespace ViewROI
  12. {
  13. public partial class HWindowTool_Smart : UserControl
  14. {
  15. public bool DispStatus { get; set; }
  16. public HImage hv_Image { get; set; }
  17. HTuple hv_Width = new HTuple();
  18. HTuple hv_Height = new HTuple();
  19. public HWindow DispHWindow
  20. {
  21. get
  22. {
  23. return this.SmartWindow.HalconWindow;
  24. }
  25. }
  26. public HWindowTool_Smart()
  27. {
  28. InitializeComponent();
  29. }
  30. public void DispImage(HObject inputImage)
  31. {
  32. if (inputImage == null || !inputImage.IsInitialized())
  33. {
  34. return;
  35. }
  36. #region 缩放图像,适应窗口
  37. //获取图像大小及纵横比
  38. HOperatorSet.GetImageSize(inputImage, out hv_Width, out hv_Height);
  39. int im_width = int.Parse(hv_Width.ToString());
  40. int im_height = int.Parse(hv_Height.ToString());
  41. double im_AspectRatio = (double)(im_width) / (double)(im_height);
  42. //获取窗口大小及纵横比
  43. int w_width = SmartWindow.Size.Width;
  44. int w_height = SmartWindow.Size.Height;
  45. double w_AspectRatio = (double)(w_width) / (double)(w_height);
  46. HOperatorSet.SetSystem("int_zooming", "false");//图像缩放之前最好将此参数设置为false.
  47. HTuple para = new HTuple("constant");
  48. HObject ho_zoomImage;
  49. HOperatorSet.GenEmptyObj(out ho_zoomImage);
  50. ho_zoomImage.Dispose();
  51. if (w_width < im_width && im_AspectRatio > w_AspectRatio)
  52. {
  53. //超宽图像
  54. HOperatorSet.ZoomImageSize(inputImage, out ho_zoomImage, w_width, w_width / im_AspectRatio, para);
  55. }
  56. else if (w_height < im_height && im_AspectRatio < w_AspectRatio)
  57. {
  58. //超高图像
  59. HOperatorSet.ZoomImageSize(inputImage, out ho_zoomImage, w_height * im_AspectRatio, w_height, para);
  60. }
  61. #endregion
  62. SmartWindow.HalconWindow.SetPart(0, 0,-2, -2);
  63. hv_Image = new HImage(inputImage);
  64. SmartWindow.HalconWindow.DispImage(hv_Image);
  65. ho_zoomImage.Dispose();
  66. hv_Width.Dispose();
  67. hv_Height.Dispose();
  68. }
  69. private void SmartWindow_HMouseMove(object sender, HalconDotNet.HMouseEventArgs e)
  70. {
  71. // this.Cursor = e.Button == MouseButtons.Left ? Cursors.Hand : Cursors.Default;
  72. if(DispStatus)
  73. {
  74. if (hv_Image != null && hv_Image.IsInitialized())
  75. {
  76. try
  77. {
  78. double positionX, positionY;
  79. string str_value;
  80. string str_position;
  81. bool _isXOut = true, _isYOut = true;
  82. HTuple channel_count;
  83. HOperatorSet.CountChannels(hv_Image, out channel_count);
  84. //SmartWindow.HalconWindow.GetMpositionSubPix(out positionY, out positionX, out button_state);
  85. positionY = e.Y;
  86. positionX = e.X;
  87. str_position = String.Format("RC: {0:0},{1:0}", positionY, positionX);
  88. _isXOut = (positionX < 0 || positionX >= hv_Width);
  89. _isYOut = (positionY < 0 || positionY >= hv_Height);
  90. if (!_isXOut && !_isYOut)
  91. {
  92. if ((int)channel_count == 1)
  93. {
  94. double grayVal;
  95. grayVal = hv_Image.GetGrayval((int)positionY, (int)positionX);
  96. str_value = String.Format("Val: {0:000}", grayVal);
  97. }
  98. else if ((int)channel_count == 3)
  99. {
  100. double grayValRed, grayValGreen, grayValBlue;
  101. HImage _RedChannel, _GreenChannel, _BlueChannel;
  102. _RedChannel = hv_Image.AccessChannel(1);
  103. _GreenChannel = hv_Image.AccessChannel(2);
  104. _BlueChannel = hv_Image.AccessChannel(3);
  105. grayValRed = _RedChannel.GetGrayval((int)positionY, (int)positionX);
  106. grayValGreen = _GreenChannel.GetGrayval((int)positionY, (int)positionX);
  107. grayValBlue = _BlueChannel.GetGrayval((int)positionY, (int)positionX);
  108. _RedChannel.Dispose();
  109. _GreenChannel.Dispose();
  110. _BlueChannel.Dispose();
  111. str_value = String.Format("Gray: ({0:000}, {1:000}, {2:000})", grayValRed, grayValGreen, grayValBlue);
  112. }
  113. else
  114. {
  115. str_value = "";
  116. }
  117. grayValueLable.Text = $"Ch{channel_count.D }, {str_position}: {str_value}";
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. //不处理
  123. }
  124. }
  125. }
  126. }
  127. private void HWindowTool_Smart_Load(object sender, EventArgs e)
  128. {
  129. this.MouseWheel += HWindowTool_Smart_MouseWheel;
  130. }
  131. private void HWindowTool_Smart_MouseWheel(object sender, MouseEventArgs e)
  132. {
  133. Point pt = SmartWindow.Location;
  134. MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, e.X - pt.X, e.Y - pt.Y, e.Delta);
  135. SmartWindow.HSmartWindowControl_MouseWheel(sender, newe);
  136. }
  137. private void HWindowTool_Smart_SizeChanged(object sender, EventArgs e)
  138. {
  139. //Point pt = SmartWindow.Location;
  140. //Point pt2 = SmartWindow.PointToScreen(pt);
  141. //this.SmartWindow.HalconWindow.SendMouseDoubleClickEvent(pt2.X+20, pt2.Y+30,(int)MouseButtons.Left);
  142. }
  143. private void tsmiDispCorr_Click(object sender, EventArgs e)
  144. {
  145. if(tsmiDispCorr.Checked)
  146. {
  147. tsmiDispCorr.Checked = false;
  148. DispStatus = false;
  149. grayValueLable.Visible = false;
  150. }
  151. else
  152. {
  153. tsmiDispCorr.Checked = true;
  154. DispStatus = true;
  155. grayValueLable.Visible = true;
  156. }
  157. }
  158. }
  159. }