首页 > 其他分享 >WPF render image, scaletransfrom and translatertransform

WPF render image, scaletransfrom and translatertransform

时间:2024-09-21 19:46:20浏览次数:9  
标签:scaler Windows System private imgIndex scaletransfrom using WPF translatertransf

<Window x:Class="WpfApp392.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:WpfApp392"
        mc:Ignorable="d"     
        WindowState="Maximized"
        KeyDown="Window_KeyDown"
        MouseDown="img_MouseDown"
        MouseMove="img_MouseMove"
        MouseUp="img_MouseUp"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Image x:Name="img" ClipToBounds="True"
               Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
               RenderOptions.BitmapScalingMode="HighQuality" 
               MouseWheel="Image_MouseWheel">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaler"/>
                    <TranslateTransform x:Name="translater"/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
    </Grid>
</Window>


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

namespace WpfApp392
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private List<string> imgsList { get; set; }
        private int imgsCount { get; set; }
        private int imgIndex { get; set; }
        private Point currentPt { get; set; }

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

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            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 void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            imgsList = Directory.GetFiles(@"../../Images")?.ToList();
            if (imgsList != null && imgsList.Any())
            {
                imgsCount = imgsList.Count();
                imgIndex = 0;
                ImgUrl = imgsList[imgIndex];
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                if (--imgIndex < 0)
                {
                    imgIndex = imgsCount - 1;
                }
            }
            else if (e.Key==Key.Down)
            {
                if (++imgIndex>=imgsCount)
                {
                    imgIndex = 0;
                }
            }
            this.Title = imgIndex.ToString();
            ImgUrl = imgsList[imgIndex];
            scaler.ScaleX = 1;
            scaler.ScaleY = 1;
            scaler.CenterX = 0;
            scaler.CenterY = 0;
            translater.X = 0;
            translater.Y = 0;
        }

        private void Image_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;
            translater.X = 0;
            translater.Y=0;
        }

        private void img_MouseDown(object sender, MouseButtonEventArgs e)
        {
            currentPt = e.GetPosition(this);
        }

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

        private void img_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if(e.ChangedButton==MouseButton.Left && e.ButtonState==MouseButtonState.Released && isMoving)
            {
                var pt = e.GetPosition(this);
                translater.X += (pt.X - currentPt.X);
                translater.Y += (pt.Y - currentPt.Y);
                isMoving = false;
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:scaler,Windows,System,private,imgIndex,scaletransfrom,using,WPF,translatertransf
From: https://www.cnblogs.com/Fred1987/p/18424429

相关文章

  • WPF Combobox switch up and down then show the big picture in the right part
    <ComboBoxx:Name="cbx"Grid.Row="0"Grid.Column="0"SelectedIndex="0"ItemsSource="{StaticResourcebooksData}"FontSize="20"......
  • WPF Combobox show Image and textblock via DataTemplate
    <ComboBoxGrid.Row="0"Grid.Column="0"SelectedIndex="0"ItemsSource="{StaticResourcebooksData}"FontSize="20"Foreground=&quo......
  • 不可不知的WPF动画(Animation)
    在WPF开发应用中,动画不仅可以引起用户的注意与兴趣,而且还使软件更加便于使用。前面几篇文章讲解了画笔(Brush),形状(Shape),几何图形(Geometry),变换(Transform)等相关内容,今天继续讲解动画相关内容和知识点,仅供学习分享使用,如有不足之处,还请指正。 什么是动画? 动画是快速循环播放一系......
  • WPF dynamically generate grid with calculated rows and columns, put the custom c
    privatevoidFillGridRandomly(){rowsColsSet=newHashSet<XY>();if(gd!=null){gd.Children.Clear();}for(inti=0;i<50;i++){while(true){XYxy=newXY();......
  • Wpf使用NLog将日志输出到LogViewer
    1LogViewerLogViewer是通过UDP传输的高性能实时log查看器。具有一下特性:通过UDP读取日志通过文件导入日志导出日志到一个文件中排序、过滤(日志树,日志等级)和查找突出显示搜索文本从UPD接收日志时忽略IP地址列表多接收器支持多种颜色主题项目地址:https://github.com/......
  • WPF Canvas show custom control with ellipse filled with image and text,peridoica
    //customcontrol<UserControlx:Class="WpfApp389.ElpImageTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • WPF ListBox ListBox.ItemTemplate DataTemplate Custom UserControl
    <ListBox.ItemTemplate><DataTemplate><local:ImageTbkUCImgUrl="{BindingDataContext.ImgUrl,RelativeSource={RelativeSourceMode=FindAncestor,AncestorType=ListBoxItem}}"U......
  • 将 WPF 窗口嵌入到 MFC 窗口中
    背景有一个现存的MFC项目,需要在里面添加新的UI界面,使用MFC开发太费劲,完全使用WPF再重写一遍,时间上不允许。可以考虑直接将WPF窗口嵌入到MFC窗口中,以下是探索过程中的一些记录。......
  • WPF 数据模板Data Template
    数据模板DataTemplate控件模板决定了数据的展示形式和用户体检,在软件UI设计中非常重要。同样数据的展示形式越来越多样化,正所谓:横看成岭侧成峰,远近高低各不同。同样的数据内容,在DataGrid中的展示是文本的列表形式,在ComboBox中是下拉框的形式。给数据披上外衣,将数据和形式解耦......
  • WPF 控件模板ControlTemplate
    <Button><Button.Template><ControlTemplate><Grid><EllipseName="faceEllipse"Width="{TemplateBindingButton.Width}"Height=&qu......