吉吉于

free

WPF 图片放大/缩小/拖动

XAML:

<Window x:Class="MouseMove.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MouseMove"
    Title="Window1" Height="600" Width="800">
    <Grid x:Name="MainPanel">
        <Grid.Resources>
            <TransformGroup x:Key="ImageTransformResource">
                <ScaleTransform />
                <TranslateTransform />
            </TransformGroup>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Column="0" x:Name="MasterImage"
                   MouseLeftButtonDown="MasterImage_MouseLeftButtonDown"
                   MouseLeftButtonUp="MasterImage_MouseLeftButtonUp"
                   MouseMove="MasterImage_MouseMove"
                   MouseWheel="MasterImage_MouseWheel">
            <Rectangle.Fill>
                <VisualBrush Transform="{StaticResource ImageTransformResource}" Stretch="Uniform">
                    <VisualBrush.Visual>
                        <Image Source="G:\图片\z.gif" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle Grid.Column="1"
                   MouseLeftButtonDown="MasterImage_MouseLeftButtonDown"
                   MouseLeftButtonUp="MasterImage_MouseLeftButtonUp"
                   MouseMove="MasterImage_MouseMove"
                   MouseWheel="MasterImage_MouseWheel">
            <Rectangle.Fill>
                <VisualBrush Transform="{StaticResource ImageTransformResource}" Stretch="Uniform">
                            <VisualBrush.Visual>
                        <Image Source="G:\图片\z.gif" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Window>

CS:;

using System;
using System.Collections.Generic;

using System.Text;
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;
using System.Diagnostics;
 
namespace MouseMove
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private bool m_IsMouseLeftButtonDown;
 
        public Window1()
        {
            InitializeComponent();
        }
 
        private void MasterImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Rectangle rectangle = sender as Rectangle;
            if (rectangle == null)
                return;
 
            rectangle.ReleaseMouseCapture();
            m_IsMouseLeftButtonDown = false;
        }
 
        private Point m_PreviousMousePoint;
        private void MasterImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Rectangle rectangle = sender as Rectangle;
            if (rectangle == null)
                return;
 
            rectangle.CaptureMouse();
            m_IsMouseLeftButtonDown = true;
            m_PreviousMousePoint = e.GetPosition(rectangle);
        }
 
        private void MasterImage_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle rectangle = sender as Rectangle;
            if (rectangle == null)
                return;
 
            if (m_IsMouseLeftButtonDown)
                DoImageMove(rectangle, e);
        }
 
        private void DoImageMove(Rectangle rectangle, MouseEventArgs e)
        {
            //Debug.Assert(e.LeftButton == MouseButtonState.Pressed);
            if (e.LeftButton != MouseButtonState.Pressed)
                return;
 
            TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
            Debug.Assert(group != null);
            TranslateTransform transform = group.Children[1] as TranslateTransform;
            Point position = e.GetPosition(rectangle);
            transform.X += position.X - m_PreviousMousePoint.X;
            transform.Y += position.Y - m_PreviousMousePoint.Y;
 
            m_PreviousMousePoint = position;
        }
 
        private void MasterImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
            Debug.Assert(group != null);
            ScaleTransform transform = group.Children[0] as ScaleTransform;
            transform.ScaleX += e.Delta * 0.001;
            transform.ScaleY += e.Delta * 0.001;
        }
    }
}

转载请注明:于哲的博客 » WPF 图片放大/缩小/拖动