首页 > 其他分享 >WPF Image zoomin zoomout move

WPF Image zoomin zoomout move

时间:2024-06-08 20:54:43浏览次数:18  
标签:scaler img zoomout void move System Windows using WPF

//xaml
<Window x:Class="WpfApp145.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApp145"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" x:Name="lbx" SelectedIndex="0" SelectionChanged="lbx_SelectionChanged" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 
                           Width="200" Height="500"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Image Grid.Column="1" x:Name="img"  Source="{Binding SelectedItem,ElementName=lbx}" 
                   ClipToBounds="True"
                  MouseDown="img_MouseDown" MouseMove="img_MouseMove" MouseUp="img_MouseUp"
                 MouseWheel="img_MouseWheel">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaler"/>
                    <TranslateTransform x:Name="translater"/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
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;
using System.IO;

namespace WpfApp145
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitLbx();
        }

        private void InitLbx()
        {
            var files = Directory.GetFiles(@"..\..\Images", "*", SearchOption.AllDirectories);
            lbx.ItemsSource = files;
        }

        Point prePoint { get; set; }
        bool isMoving { get; set; }
        private void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
            prePoint = e.GetPosition(img);
        }

        private void img_MouseMove(object sender, MouseEventArgs e)
        {
            isMoving = true;
        }

        private void img_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (isMoving && e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Released)
            {
                Point newPoint = e.GetPosition(img);
                translater.X += newPoint.X - prePoint.X;
                translater.Y += newPoint.Y - prePoint.Y;
                isMoving = false;
            }
        }

        private void img_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                scaler.ScaleX *= 1.2;
                scaler.ScaleY *= 1.2;
            }
            else
            {
                scaler.ScaleX /= 1.2;
                scaler.ScaleY /= 1.2;
            }
            scaler.CenterX = e.GetPosition(img).X;
            scaler.CenterY = e.GetPosition(img).Y;
        }

        private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            scaler.ScaleX = 1.0;
            scaler.ScaleY = 1.0;
            translater.X = 0;
            translater.Y = 0;
        }
    }
}

 

 

 

 

 

 

 

 

标签:scaler,img,zoomout,void,move,System,Windows,using,WPF
From: https://www.cnblogs.com/Fred1987/p/18238937

相关文章

  • WPF ListBox ListBox.ItemTemplate DataTemplate
    <Windowx:Class="WpfApp144.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF master detail view
    <Windowx:Class="WpfApp143.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF RepeatButton
    是一个特殊的按钮,用于在用户长按或连续点击时重复执行特定动作。它通常用于需要重复执行某个操作的场景。常用属性描述Delay用于获取或设置RepeatButton在开始重复之前被按下时等待的时间(以毫秒为单位)。该值必须为非负数。Interval用于获取或设置开始重复后重复之......
  • WPF button mouseover background change color
    <Applicationx:Class="WpfApp142.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-nam......
  • 在WPF程序中如何实现主题,字体的切换
    在wpf程序中如何实现主题变换,首先我们需要先思考主题切换都更改了一些什么,无非就是字体颜色,背景颜色等一些资源当我们了解了主题变换的本质后就能够进一步去实现它,众所周知,在wpf中我们可以通过资源绑定的形式去实现颜色,字体,样式等一些资源绑定,在wpf中如果有相同的key通常会绑......
  • 【WPF】Dispatcher 与消息循环
    这一期的话题有点深奥,不过按照老周一向的作风,尽量讲一些人鬼都能懂的知识。咱们先来整个小活开开胃,这个小活其实老周在N年前写过水文的,常阅读老周水文的伙伴可能还记得。通常,咱们按照正常思路构建的应用程序,第一个启动的线程为主线程,而且还是UI线程(当然,WPF默认会创建辅助线......
  • WPF grid column resize via GridSpitter, when you can drag to enlarge or shrink t
    <Windowx:Class="WpfApp137.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF,图表控件
    开源代码地址:https://github.com/bearhanQ/WPFFramework;QQ群:332035933;<UserControlx:Class="WpfBootstrap.View.ChartsView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://......
  • wpf datagrid绑定行选中状态
    样式如下<DataGridMargin="0,6,0,0"HeadersVisibility="All"RowHeaderWidth="60"HorizontalScrollBarVisibility="Visible"AutoGenerateColumns="False"ItemsSource="{BindingDispl......
  • 界面控件Telerik UI for WPF中文教程 - 用RadSvgImage升级应用程序UI
    TelerikUIforWPF拥有超过100个控件来创建美观、高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序。UIforWPF支持MVVM、触摸等,创建的应用程序可靠且结构良好,非常容易维护,其直观的API将无缝地集成VisualStudio工具箱中。TelerikUIforWPF中的RadSvgImage组件使......