PictureDisplayControl.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using OxyPlot;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.Eventing.Reader;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace TeamAAS_VP.Controls
  18. {
  19. /// <summary>
  20. /// PictureDisplayControl.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class PictureDisplayControl : UserControl
  23. {
  24. private bool _startMove;
  25. private Point _start;
  26. //private bool _isFirst = true;
  27. public PictureDisplayControl()
  28. {
  29. InitializeComponent();
  30. Loaded+=(s, e) =>
  31. {
  32. FitImageToControl();
  33. };
  34. }
  35. public BitmapSource Image
  36. {
  37. get { return (BitmapSource)GetValue(ImageProperty); }
  38. set { SetValue(ImageProperty, value);
  39. if (value != null)
  40. {
  41. FitImageToControl();
  42. }
  43. //if (_isFirst && value!=null)
  44. //{
  45. // FitImageToControl();
  46. // _isFirst = false;
  47. //}
  48. }
  49. }
  50. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  51. public static readonly DependencyProperty ImageProperty =
  52. DependencyProperty.Register("Image", typeof(BitmapSource), typeof(PictureDisplayControl), new PropertyMetadata(null, new PropertyChangedCallback(ImageValueChanged)));
  53. private static void ImageValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  54. {
  55. PictureDisplayControl obj = (PictureDisplayControl)d;
  56. if (obj != null)
  57. {
  58. obj.Update(e.NewValue as BitmapSource);
  59. }
  60. }
  61. private void Update(BitmapSource bitmap)
  62. {
  63. App.Current.Dispatcher.Invoke(() =>
  64. {
  65. if (bitmap != null)
  66. {
  67. this.Img.Source = bitmap;
  68. FitImageToControl();
  69. }
  70. else
  71. {
  72. this.Img.Source = bitmap;
  73. }
  74. });
  75. }
  76. private void Viewbox_MouseWheel(object sender, MouseWheelEventArgs e)
  77. {
  78. var centerPt = e.GetPosition(Box);
  79. ChangSize(e.Delta > 0, Box, centerPt.X, centerPt.Y);
  80. e.Handled = true; //避免缩放时同时影响滚动条;
  81. }
  82. private void Viewbox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  83. {
  84. if (Box.IsMouseCaptured) return;
  85. Box.CaptureMouse();
  86. _start = e.MouseDevice.GetPosition(Sv);
  87. e.Handled = true;
  88. _startMove = true;
  89. }
  90. private void Viewbox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  91. {
  92. Box.ReleaseMouseCapture();
  93. _startMove = false;
  94. }
  95. private void Viewbox_PreviewMouseMove(object sender, MouseEventArgs e)
  96. {
  97. if (!Box.IsMouseCaptured || !_startMove) return;
  98. var point = e.MouseDevice.GetPosition(Sv);
  99. var m = Box.RenderTransform.Value;
  100. //计算x偏移量
  101. m.OffsetX = m.OffsetX + point.X - _start.X;
  102. //计算y偏移量
  103. m.OffsetY = m.OffsetY + point.Y - _start.Y;
  104. Box.RenderTransform = new MatrixTransform(m);
  105. //给鼠标起点和图片临时位置重新赋值
  106. _start = point;
  107. }
  108. private void ChangSize(bool big, FrameworkElement ctrl, double centerX, double centerY)
  109. {
  110. var m = ((MatrixTransform)ctrl.RenderTransform).Matrix;
  111. if (big)
  112. {
  113. m.ScaleAtPrepend(1.1, 1.1, centerX, centerY);
  114. }
  115. else
  116. {
  117. m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, centerX, centerY);
  118. }
  119. ctrl.RenderTransform = new MatrixTransform(m);
  120. }
  121. private void FitImageToControl()
  122. {
  123. if (Image==null)
  124. {
  125. return;
  126. }
  127. var imageWidth = Image.PixelWidth;
  128. var imageHeight = Image.PixelHeight;
  129. var controlWidth = this.ActualWidth;
  130. var controlHeight = this.ActualHeight;
  131. double scaleX = controlWidth / imageWidth;
  132. double scaleY = controlHeight / imageHeight;
  133. double scale = Math.Min(scaleX, scaleY);
  134. var m = new Matrix();
  135. m.Scale(scale, scale);
  136. m.Translate((controlWidth - imageWidth * scale) / 2, (controlHeight - imageHeight * scale) / 2);
  137. Box.RenderTransform = new MatrixTransform(m);
  138. }
  139. }
  140. }