首页 > 其他分享 >WPF Image ZoomIn ZoomOut

WPF Image ZoomIn ZoomOut

时间:2024-05-23 15:51:19浏览次数:12  
标签:string ZoomOut Image System private Windows using WPF public

//xaml
<Window x:Class="WpfApp109.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:WpfApp109"
        mc:Ignorable="d" WindowState="Maximized"
        Title="{Binding ZoomLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="450" Width="800">
    <Grid x:Name="gd" > 
        <Image Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MouseWheel="gd_MouseWheel">
            <Image.RenderTransform>
                <ScaleTransform ScaleX="{Binding ZoomLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                ScaleY="{Binding ZoomLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                </ScaleTransform>
            </Image.RenderTransform>
        </Image>

    </Grid>
</Window>


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;
using System.ComponentModel;

namespace WpfApp109
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            InitContent();
            this.DataContext= this;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));  
            }
        }

        private string imgUrl;
        public string ImgUrl
        {
            get
            {
                return imgUrl;
            }
            set
            {
                if (value != imgUrl)
                {
                    imgUrl = value;
                    OnPropertyChanged(nameof(ImgUrl));
                }
            }
        }

        private double zoomLevel = 1.0;
        public double ZoomLevel
        {
            get
            {
                return zoomLevel;
            }
            set
            {
                if(value!=zoomLevel) 
                {
                    zoomLevel = value;
                    OnPropertyChanged(nameof(ZoomLevel));
                }
            }
        }
        

        private void InitContent()
        {
            string dir= @"..\..\Images\cl14.jpg";
            string fullDir = System.IO.Path.GetFullPath(dir);
            if(File.Exists(fullDir))
            {
                ImgUrl = fullDir;
            }
        } 

        private void gd_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if(e.Delta>0)
            {
                ZoomLevel *= 1.2;
            }
            else
            {
                ZoomLevel /= 1.2;
            }
        }
    }
}

 

 

 

 

 

 

 

 

标签:string,ZoomOut,Image,System,private,Windows,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18208638

相关文章

  • 推荐一个WPF仪表盘开源控件
    前段时间,做服务器端监控系统,为了界面好看,采用WPF。硬件相关监控,比如CPU、内存等,想用仪表盘控件。网上找了很多这种控件,基本上都是第三方商业控件(虽然很漂亮,不过得money...)。最后在CodeProject上找到了一款还不错的开源的仪表盘控件CircularGauge。用了下该控件,感觉还不错......
  • WPF插件之 - PropertyChanged.Fody使用详解
    总目录文章目录总目录一、PropertyChanged.Fody是什么?二、PropertyChanged.Fody的安装三、PropertyChanged.Fody的功能1.特性1实现属性通知的功能2通知其他属性4不进行属性通知3指定属性更改时将调用的方法5设置当前属性依赖的属性6不检查是否相等7DoNotSetChangedAttribu......
  • wpf 双屏显示问题
    //在WPF中处理双屏显示问题,通常需要确保应用程序能够识别两个显示器,并在每个显示器上正确渲染内容。以下是一个简化的示例,展示如何在WPF应用程序中设置窗口,使其跨越两个显示器: usingSystem;usingSystem.Windows;usingSystem.Windows.Forms;publicpartialclassMa......
  • WPF多显示器问题 - WindowState
    标签 wpf multiple-monitors一段时间以来,我一直试图让我的WPF应用程序跨越多个监视器,并且几乎可以正常工作。当我设置以下行时,问题似乎出现了:win1.WindowState=WindowState.Maximized这会导致应用程序仅跨越主屏幕。我的代码如下:publicpartialclassApp:App......
  • C#WPF的多屏显示问题
    如果想让窗口在第二个屏幕中显示publicMainWindow(){InitializeComponent();Screen[]_screens=Screen.AllScreens;Screens=Screen.AllScreens[1];System.Drawing.Rectanglerect=s.WorkingArea;......
  • wpf 动画显示隐藏_[UWP]用Win2D和CompositionAPI实现文字的发光效果,并制作动画
    weixin_39880899于2020-12-1109:26:23发布阅读量521 收藏点赞数文章标签: wpf动画显示隐藏  1.成果 献祭了周末的晚上,成功召唤出了上面的番茄钟。正当我在感慨“不愧是Shadow大人,这难道就是传说中的五彩斑斓的黑?”“那才不是什么阴影效果,那是......
  • WPF炫酷UI及动画
        偶然看见了一张图,感觉挺好看的,花了点时间将他转化成了我代码仓库的一部分。虽然不难但也费时间。其中除了背景是百度的一张底图,其他所有内容均通过WPF的Path、Line、TextBlock、Border以及DoubleAnimation来实现。效果如下:​纯黑色背景也还蛮好看的。​   ......
  • Wpf UI框架 MaterialDesign 的使用记录
    近期公司有桌面客户端的开发需求,并且对样式和界面反馈有一定的要求,对比各种开源UI框架后确认使用MaterialDesign。1、引入框架MaterialDesignThemes,注意下对应的版本号,我用的2.6.0的。原因嘛视频教程就是这个版本。  2、App.xaml中引用样式文件注释下面的可以先不用加,后面......
  • WPF 给类库设置设计时使用的资源字典
    WPF给类库设置设计时使用的资源字典 在开发多语言版本时,我将界面显示的文本保存在语言资源zh.xaml和en.xaml中,但程序启动,加载语言资源时是外部的配置文件决定的,因此语言资源我无法添加在App.xaml文件中,而开发单个XAML界面时,设计器将会因为找不到资源文件的存在,而拿不到资......
  • WPF 实现触摸滑动功能
    自定义ScrollViewer的Touch事件--触摸上下移动ScrollViewer滚动到指定位置 12345678910111213141516171819202122232425262728double mPointY;//触摸点的Y坐标double mOffsetY;//滚动条当前位置bool mIsTouch= false;//是否触......