zoukankan      html  css  js  c++  java
  • wpf 图像浏览(平移,缩放)

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            x:Class="WpfApp2.MainWindow"
            mc:Ignorable="d"
            Title="MainWindow" Height="800" Width="1000">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
            <Border x:Name="outside" Background="Black" 
                    PreviewMouseDown="outsidewrapper_PreviewMouseDown" 
                    PreviewMouseMove="outsidewrapper_PreviewMouseMove" 
                    PreviewMouseUp="outside_PreviewMouseUp"
                    PreviewMouseWheel="outside_PreviewMouseWheel"
                    ClipToBounds="True">
    
                <Canvas x:Name="inside" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Border}}}" 
                        Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Border}}}">
                    <Canvas.RenderTransform>
                        <TransformGroup/>
                    </Canvas.RenderTransform>
                    <Image x:Name="myImage" Source="{Binding IamgeSource}" Stretch="Uniform" RenderOptions.BitmapScalingMode="NearestNeighbor" >
                        <Image.RenderTransform >
                            <ScaleTransform x:Name="ImageScale"/>
                        </Image.RenderTransform>
                    </Image>
                </Canvas>
            </Border>
            <Button Click="BtnClickLoadPic" Grid.Row="2" HorizontalAlignment="Right" Content="加载图片" />
            <Button Click="BtnClickFullScreen" Grid.Row="2" HorizontalAlignment="Right" Content="   全屏   " Margin="0,0,60,0" />
    
        </Grid>
    
    </Window>
    前台代码
    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Messaging;
    using LiveCharts;
    using LiveCharts.Wpf;
    using MathNet.Numerics.LinearAlgebra.Double;
    using Microsoft.Win32;
    //using MathNet.Numerics.LinearAlgebra.Complex;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    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 WpfApp2
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            Point previousPoint;
            bool isTranslateStart = false;
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void outsidewrapper_PreviewMouseDown(object sender, MouseButtonEventArgs e)
            {
                if (e.MiddleButton == MouseButtonState.Pressed && e.LeftButton == MouseButtonState.Released && e.RightButton == MouseButtonState.Released)
                {
                    previousPoint = e.GetPosition(outside);
                    isTranslateStart = true;
                }
                e.Handled = true;
            }
    
            private void outsidewrapper_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                if (e.MiddleButton == MouseButtonState.Pressed && e.LeftButton == MouseButtonState.Released && e.RightButton == MouseButtonState.Released)
                {
                    if (isTranslateStart)
                    {
                        Point currentPoint = e.GetPosition(outside);  //不能用 inside,必须用outside
                        System.Windows.Vector v = currentPoint - previousPoint;
                        TransformGroup tg = inside.RenderTransform as TransformGroup;
                        tg.Children.Add(new TranslateTransform(v.X, v.Y));  //centerX和centerY用外部包装元素的坐标,不能用内部被变换的Canvas元素的坐标
                        previousPoint = currentPoint;
                    }
                }
                e.Handled = true;
            }
    
            private void outside_PreviewMouseUp(object sender, MouseButtonEventArgs e)
            {
                if (e.MiddleButton == MouseButtonState.Pressed && e.LeftButton == MouseButtonState.Released && e.RightButton == MouseButtonState.Released)
                {
                    if (isTranslateStart)
                    {
                        isTranslateStart = false;
                    }
                }
                e.Handled = true;
            }
            private void outside_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
            {
                Point currentPoint = e.GetPosition(outside);  //不能用 inside,必须用outside
                TransformGroup tg = inside.RenderTransform as TransformGroup;
                double s = ((double)e.Delta) / 1000.0 + 1.0;
                if (myImage.Height * tg.Value.M22 < 200 || myImage.Width * tg.Value.M11 < 200)
                {
                    if(s > 1.0)//当图像像素宽度和高度小于200的时候只能放大,不再缩小
                        tg.Children.Add(new ScaleTransform(s, s, currentPoint.X, currentPoint.Y));
                }
                else
                    tg.Children.Add(new ScaleTransform(s, s, currentPoint.X, currentPoint.Y));
                e.Handled = true;
            }
    
            private void BtnClickLoadPic(object sender, RoutedEventArgs e)
            {
                // Instantiate the dialog box
                var dlg = new Microsoft.Win32.OpenFileDialog
                {
                    FileName = "",
                    Filter = "All Files|*.*|PNG Files|*.png|JPG Files|*.jpg|TIF Files|*.tif"
                };
                var result = dlg.ShowDialog();
                // Process open file dialog box results
                if (result == true)
                {
                    BitmapImage bitmapImage = new BitmapImage(new Uri(dlg.FileName));
                    myImage.Source = bitmapImage;
                    myImage.Height = bitmapImage.PixelHeight;//为了得到像素对应的坐标
                    myImage.Width = bitmapImage.PixelWidth;//为了得到像素对应的坐标
                }
            }
    
            private void BtnClickFullScreen(object sender, RoutedEventArgs e)
            {
                TransformGroup tg = inside.RenderTransform as TransformGroup;
                System.Windows.Media.Matrix value = new System.Windows.Media.Matrix();
                value.M11 = outside.ActualWidth / myImage.Width / tg.Value.M11;//缩放倍数为Border的尺寸
                value.M22 =  outside.ActualHeight / myImage.Height / tg.Value.M22;
                value.OffsetX = (0 - tg.Value.OffsetX) * value.M11;//调整偏移XY到(0,0)
                value.OffsetY = (0 - tg.Value.OffsetY) * value.M22;
                MatrixTransform matrixTransform = new MatrixTransform(value);//添加矩阵变换
                tg.Children.Add(matrixTransform);
                e.Handled = true;
            }
        }
    }
    后台代码

  • 相关阅读:
    生成排列与生成子集
    赛后总结AtCoder Beginner Contest 090(Beginner)
    树状数组笔记
    论怎么记住tarjan的板子
    tarjan缩点-受欢迎的牛-笔记
    tarjan模板(%%%hzwer)-2.0
    tarjan模板(%%%hzwer)
    匈牙利算法学习笔记
    最短路-Car的旅行路线
    数据结构 笔记1 搜索树
  • 原文地址:https://www.cnblogs.com/lizhiqiang0204/p/15090738.html
Copyright © 2011-2022 走看看