| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using OxyPlot;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.Eventing.Reader;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace TeamAAS_VP.Controls
- {
- /// <summary>
- /// PictureDisplayControl.xaml 的交互逻辑
- /// </summary>
- public partial class PictureDisplayControl : UserControl
- {
- private bool _startMove;
- private Point _start;
- //private bool _isFirst = true;
- public PictureDisplayControl()
- {
- InitializeComponent();
- Loaded+=(s, e) =>
- {
- FitImageToControl();
- };
- }
- public BitmapSource Image
- {
- get { return (BitmapSource)GetValue(ImageProperty); }
- set { SetValue(ImageProperty, value);
- if (value != null)
- {
- FitImageToControl();
- }
- //if (_isFirst && value!=null)
- //{
- // FitImageToControl();
- // _isFirst = false;
- //}
- }
- }
- // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ImageProperty =
- DependencyProperty.Register("Image", typeof(BitmapSource), typeof(PictureDisplayControl), new PropertyMetadata(null, new PropertyChangedCallback(ImageValueChanged)));
- private static void ImageValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- PictureDisplayControl obj = (PictureDisplayControl)d;
- if (obj != null)
- {
- obj.Update(e.NewValue as BitmapSource);
- }
- }
- private void Update(BitmapSource bitmap)
- {
- App.Current.Dispatcher.Invoke(() =>
- {
- if (bitmap != null)
- {
- this.Img.Source = bitmap;
- FitImageToControl();
- }
- else
- {
- this.Img.Source = bitmap;
- }
- });
- }
- private void Viewbox_MouseWheel(object sender, MouseWheelEventArgs e)
- {
- var centerPt = e.GetPosition(Box);
- ChangSize(e.Delta > 0, Box, centerPt.X, centerPt.Y);
- e.Handled = true; //避免缩放时同时影响滚动条;
- }
- private void Viewbox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (Box.IsMouseCaptured) return;
- Box.CaptureMouse();
- _start = e.MouseDevice.GetPosition(Sv);
- e.Handled = true;
- _startMove = true;
- }
- private void Viewbox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- Box.ReleaseMouseCapture();
- _startMove = false;
- }
- private void Viewbox_PreviewMouseMove(object sender, MouseEventArgs e)
- {
- if (!Box.IsMouseCaptured || !_startMove) return;
- var point = e.MouseDevice.GetPosition(Sv);
- var m = Box.RenderTransform.Value;
- //计算x偏移量
- m.OffsetX = m.OffsetX + point.X - _start.X;
- //计算y偏移量
- m.OffsetY = m.OffsetY + point.Y - _start.Y;
- Box.RenderTransform = new MatrixTransform(m);
- //给鼠标起点和图片临时位置重新赋值
- _start = point;
- }
- private void ChangSize(bool big, FrameworkElement ctrl, double centerX, double centerY)
- {
- var m = ((MatrixTransform)ctrl.RenderTransform).Matrix;
- if (big)
- {
- m.ScaleAtPrepend(1.1, 1.1, centerX, centerY);
- }
- else
- {
- m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, centerX, centerY);
- }
- ctrl.RenderTransform = new MatrixTransform(m);
- }
- private void FitImageToControl()
- {
- if (Image==null)
- {
- return;
- }
- var imageWidth = Image.PixelWidth;
- var imageHeight = Image.PixelHeight;
- var controlWidth = this.ActualWidth;
- var controlHeight = this.ActualHeight;
- double scaleX = controlWidth / imageWidth;
- double scaleY = controlHeight / imageHeight;
- double scale = Math.Min(scaleX, scaleY);
- var m = new Matrix();
- m.Scale(scale, scale);
- m.Translate((controlWidth - imageWidth * scale) / 2, (controlHeight - imageHeight * scale) / 2);
- Box.RenderTransform = new MatrixTransform(m);
- }
- }
- }
|