|
@@ -19,153 +19,15 @@ public partial class SiteDocumentView : UserControl
|
|
|
public SiteDocumentView()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
- }
|
|
|
|
|
|
- // private Bitmap? _bitmap = null;
|
|
|
- //
|
|
|
- // protected override void OnDataContextChanged(EventArgs e)
|
|
|
- // {
|
|
|
- // base.OnDataContextChanged(e);
|
|
|
- // if (DataContext is SiteDocumentViewModel viewModel)
|
|
|
- // {
|
|
|
- // viewModel.DataLoaded += (data) =>
|
|
|
- // {
|
|
|
- // var _bmpData = PlatformTools.PdfRenderer.RenderPdf(data,0,300);
|
|
|
- // using var ms = new MemoryStream(_bmpData);
|
|
|
- // _bitmap = new Bitmap(ms);
|
|
|
- // //CheckAndLoadPdf();
|
|
|
- // };
|
|
|
- // }
|
|
|
- // }
|
|
|
- //
|
|
|
- protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
- {
|
|
|
- base.OnAttachedToVisualTree(e);
|
|
|
- _image.AddHandler(Gestures.PinchEvent, PinchHandler);
|
|
|
- _image.AddHandler(Gestures.PinchEndedEvent, PinchEndedHandler);
|
|
|
- }
|
|
|
-
|
|
|
- private void OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
|
|
|
- {
|
|
|
- if (e.Sender == _scroll && e.Property == BoundsProperty && e.NewValue is Rect rect)
|
|
|
+ _image.GetPropertyChangedObservable(Image.SourceProperty).Subscribe(x =>
|
|
|
{
|
|
|
- _minHeight = rect.Height;
|
|
|
- _minWidth = rect.Width;
|
|
|
- }
|
|
|
- else if (e.Sender == _image && e.Property == Image.SourceProperty)
|
|
|
- {
|
|
|
-
|
|
|
- if (e.NewValue is Bitmap bitmap)
|
|
|
+ if (_image.Source is not null)
|
|
|
{
|
|
|
-
|
|
|
- var maxSize = ScaleToFit(_minWidth, _minHeight, bitmap.Size.Width, bitmap.Size.Height);
|
|
|
-
|
|
|
- _maxHeight = maxSize.ScaledHeight;
|
|
|
- _maxWidth = maxSize.ScaledWidth;
|
|
|
-
|
|
|
- var initialsize = ScaleToFit(bitmap.Size.Width, bitmap.Size.Height, _minWidth, _minHeight);
|
|
|
-
|
|
|
- _currentHeight = initialsize.ScaledHeight;
|
|
|
- _currentWidth = initialsize.ScaledWidth;
|
|
|
-
|
|
|
- ScaleImage(_scroll.Offset, 1.0, false);
|
|
|
-
|
|
|
+ _image.Width = _image.Source.Size.Width;
|
|
|
+ _image.Height = _image.Source.Size.Height;
|
|
|
}
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private double _minHeight;
|
|
|
- private double _minWidth;
|
|
|
-
|
|
|
- private double _currentHeight;
|
|
|
- private double _currentWidth;
|
|
|
-
|
|
|
- private double _maxHeight;
|
|
|
- private double _maxWidth;
|
|
|
-
|
|
|
- public (double ScaledWidth, double ScaledHeight) ScaleToFit(
|
|
|
- double originalWidth,
|
|
|
- double originalHeight,
|
|
|
- double containerWidth,
|
|
|
- double containerHeight)
|
|
|
- {
|
|
|
- // Calculate the scale factors for width and height
|
|
|
- double widthScale = containerWidth / originalWidth;
|
|
|
- double heightScale = containerHeight / originalHeight;
|
|
|
-
|
|
|
- // Use the smaller scale factor to maintain aspect ratio
|
|
|
- double scale = Math.Min(widthScale, heightScale);
|
|
|
-
|
|
|
- // Calculate the new dimensions
|
|
|
- double scaledWidth = originalWidth * scale;
|
|
|
- double scaledHeight = originalHeight * scale;
|
|
|
-
|
|
|
- return (scaledWidth, scaledHeight);
|
|
|
- }
|
|
|
-
|
|
|
- private void ScaleImage(Vector origin, double scale, bool reset)
|
|
|
- {
|
|
|
- var newHeight = _currentHeight * scale;
|
|
|
- var newWidth = _currentWidth * scale;
|
|
|
-
|
|
|
- // var deltaHeight = newHeight - _currentHeight;
|
|
|
- // var deltaWidth = newWidth - _currentWidth;
|
|
|
- // var _offsetX = _scroll.Offset.X + (deltaWidth / 2.0);
|
|
|
- // var _offsetY = _scroll.Offset.Y + (deltaHeight / 2.0);
|
|
|
- _scroll.Offset = origin;
|
|
|
-
|
|
|
- _image.Height = _currentHeight * scale;
|
|
|
- _image.Width = _currentWidth * scale;
|
|
|
- if (reset)
|
|
|
- {
|
|
|
- _currentHeight = _image.Height;
|
|
|
- _currentWidth = _image.Width;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // private void CheckAndLoadPdf()
|
|
|
- // {
|
|
|
- //
|
|
|
- //
|
|
|
- // }
|
|
|
-
|
|
|
- // private void OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
|
|
|
- // {
|
|
|
- // if (e.Sender is ScrollViewer && e.Property == ScrollViewer.BoundsProperty)
|
|
|
- // CheckAndLoadPdf();
|
|
|
- //
|
|
|
- // else if (e.Sender is Image && e.Property == Image.SourceProperty)
|
|
|
- // {
|
|
|
- // CheckAndLoadPdf();
|
|
|
- // }
|
|
|
- //
|
|
|
- // }
|
|
|
-
|
|
|
- private void PinchHandler(object? sender, PinchEventArgs e)
|
|
|
- {
|
|
|
-
|
|
|
- ScaleImage(e.ScaleOrigin, e.Scale, false);
|
|
|
- e.Handled = true;
|
|
|
- }
|
|
|
-
|
|
|
- private void PinchEndedHandler(object? sender, PinchEndedEventArgs e)
|
|
|
- {
|
|
|
- _currentHeight = _image.Height;
|
|
|
- _currentWidth = _image.Width;
|
|
|
- e.Handled = true;
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
- private void ZoomInClick(object? sender, RoutedEventArgs e)
|
|
|
- {
|
|
|
- ScaleImage(_scroll.Offset, 1.25, true);
|
|
|
- }
|
|
|
-
|
|
|
- private void ZoomOutClick(object? sender, RoutedEventArgs e)
|
|
|
- {
|
|
|
- ScaleImage(_scroll.Offset, 0.80, true);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|