PlcPointParams.xaml.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.Dimensioning;
  3. using System;
  4. using System.Windows.Controls;
  5. using TeamAAS_VP.ViewModels.Product;
  6. namespace TeamAAS_VP.Views.Product
  7. {
  8. /// <summary>
  9. /// Interaction logic for PlcPointParams
  10. /// </summary>
  11. public partial class PlcPointParams : UserControl
  12. {
  13. PlcPointParamsViewModel VM;
  14. public PlcPointParams()
  15. {
  16. InitializeComponent();
  17. VM = DataContext as PlcPointParamsViewModel;
  18. VM.PropertyChanged += VM_PropertyChanged;
  19. this.display.HorizontalScrollBar = false;
  20. this.display.VerticalScrollBar = false;
  21. this.display.AutoFit = true;
  22. this.display.BackColor = System.Drawing.SystemColors.ActiveCaption;
  23. }
  24. private int _imageWidth;
  25. private int _imageHeight;
  26. private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  27. {
  28. if (e.PropertyName == "Image")
  29. {
  30. if (this.display.Image == null)
  31. {
  32. this.display.Image = VM.Image;
  33. //图像中心点坐标
  34. _imageWidth = this.display.Image.Width;
  35. _imageHeight = this.display.Image.Height;
  36. CogGraphicCollection graphic = new CogGraphicCollection();
  37. CogCreateLineTool line1 = new CogCreateLineTool();
  38. CogCreateLineTool line2 = new CogCreateLineTool();
  39. line1.InputImage = this.display.Image;
  40. line2.InputImage = this.display.Image;
  41. line1.Line.X = _imageWidth / 2;
  42. line1.Line.Y = _imageHeight / 2;
  43. line1.Line.Rotation = 0;
  44. line2.Line.X = _imageWidth / 2;
  45. line2.Line.Y = _imageHeight / 2;
  46. line2.Line.Rotation = Math.PI / 180 * 90;
  47. line1.Run();
  48. line2.Run();
  49. graphic.Add(line1.GetOutputLine());
  50. graphic.Add(line2.GetOutputLine());
  51. this.display.StaticGraphics.Clear();
  52. this.display.StaticGraphics.AddList(graphic, "");
  53. }
  54. else
  55. {
  56. this.display.Image = VM.Image;
  57. //如果图像的长宽发生变化,则重新绘制十字线
  58. if (_imageWidth != this.display.Image.Width || _imageHeight != this.display.Image.Height)
  59. {
  60. _imageWidth = this.display.Image.Width;
  61. _imageHeight = this.display.Image.Height;
  62. CogGraphicCollection graphic = new CogGraphicCollection();
  63. CogCreateLineTool line1 = new CogCreateLineTool();
  64. CogCreateLineTool line2 = new CogCreateLineTool();
  65. line1.InputImage = this.display.Image;
  66. line2.InputImage = this.display.Image;
  67. line1.Line.X = _imageWidth / 2;
  68. line1.Line.Y = _imageHeight / 2;
  69. line1.Line.Rotation = 0;
  70. line2.Line.X = _imageWidth / 2;
  71. line2.Line.Y = _imageHeight / 2;
  72. line2.Line.Rotation = Math.PI / 180 * 90;
  73. line1.Run();
  74. line2.Run();
  75. graphic.Add(line1.GetOutputLine());
  76. graphic.Add(line2.GetOutputLine());
  77. this.display.StaticGraphics.Clear();
  78. this.display.StaticGraphics.AddList(graphic, "");
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }