首页 > 其他分享 >WPF Image show picture in high resolution periodically via System.Timers.Timer

WPF Image show picture in high resolution periodically via System.Timers.Timer

时间:2024-09-24 23:23:38浏览次数:8  
标签:picture via show Windows imgsList System private using public

<Window x:Class="WpfApp411.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:WpfApp411"
        mc:Ignorable="d"
        WindowState="Maximized"
        WindowStyle="None"
        KeyDown="Window_KeyDown"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Image Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
               RenderOptions.BitmapScalingMode="Fant"
               />
        <TextBlock Text="{Binding Num,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                   Foreground="Red"
                   FontSize="50"
                   HorizontalAlignment="Right"
                   VerticalAlignment="Bottom"
                   Width="200"
                   Panel.ZIndex="2"/>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Diagnostics.Eventing.Reader;

namespace WpfApp411
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        public MainWindow()
        {
            InitializeComponent();
            imgsList = new List<string>(Directory.GetFiles(@"../../Images"));
            imgsCount = imgsList.Count;
            if (imgsCount > 0)
            {
                ImgUrl = imgsList[imgIdx];
                System.Timers.Timer tmr = new System.Timers.Timer();
                tmr.Elapsed += Tmr_Elapsed;
                tmr.Interval = 10;
                tmr.Start();
                this.DataContext = this;
            }
        }

        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if(isPaused)
            {
                return;
            }

            if (++imgIdx >= imgsCount)
            {
                imgIdx = 0;
            }
            ImgUrl = imgsList[imgIdx];
            ++Num;
        }

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

        private List<string> imgsList { get; set; }
        private int imgsCount = 0;
        private bool isPaused = false;

        private int imgIdx = 0;

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

        private int num = 0;
        public int Num
        {
            get
            {
                return num;
            }
            set
            {
                if (value != num)
                {
                    num = value;
                    OnPropertyChanged(nameof(Num));
                }
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                isPaused = !isPaused;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C)
            {
                var msgResult = MessageBox.Show("Are you sure to exit this window?", "Exit", MessageBoxButton.YesNo,
                    MessageBoxImage.Question, MessageBoxResult.Yes);
                if (msgResult == MessageBoxResult.Yes)
                {
                    this.Close();
                }
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

标签:picture,via,show,Windows,imgsList,System,private,using,public
From: https://www.cnblogs.com/Fred1987/p/18430317

相关文章

  • 论文研读——《RF-Diffusion: Radio Signal Generation via Time-Frequency Diffusion
    本文的是有关无线电信号生成的一篇文章。目录论文简介名词补充现有RF数据生成模型论文贡献RF-Diffusion时频扩散时频扩散——正向销毁过程时频扩散——正向销毁过程时频扩散——逆向恢复过程  时频扩散——条件生成分层扩散Transformer分层扩散Transformer——......
  • 《NET CLR via C#》---第十二章(泛型)
    泛型(generic)是CLR和编程语言提供的一种特殊机制,它支持另一种形式的代码重用,即“算法重用”。简单来说,开发人员先定义好算法,必然排序、搜索、交换、比较或者转换等。但是,定义算法的开发人员并不设定该算法要操作什么数据类型;该算法可广泛地应用于不同类型的对象。泛型为开发人员......
  • Aviator是什么
        Aviator是一个用于Java的高性能表达式引擎,它可以解析并执行表达式,广泛应用于各种需要计算逻辑或动态决策的场景,高性能,简单易用,强大的表达语言能力。使用场景:(1)规则引擎:可以作为规则引擎的一部分,动态计算规则的结果。(2)动态查询:在复杂的查询或获取数据时,通过表......
  • WPF Image automatically display image via System.Timers.Timer ,pause and resume,
    <Windowx:Class="WpfApp408.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • Python可视化过程中.pictures.add这里一直报错,不明原因
    大家好,我是Python进阶者。一、前言前几天在Python白银交流群【沐子山树】问了一个Python可视化的问题,问题如下:importmatplotlib.pyplotasplt#创建一个简单的图表fig,ax=plt.subplots()ax.plot([1,2,3,4])ax.set_ylabel('somenumbers')#保存图表为PNG文件temp......
  • WPF StatusBar update periodically via System.Timers.Timer
    //xaml<Windowx:Class="WpfApp406.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • ListBox show image and refresh automatically via System.Timers.Timer per 1 milli
    <Windowx:Class="WpfApp403.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • Paper Reading(1)-VarietySound: Timbre-Controllable Video to Sound Generation via U
    VarietySound:Timbre-ControllableVideotoSoundGenerationviaUnsupervisedInformationDisentanglement来源:https://doi.org/10.48550/arXiv.2211.10666https://conferencedemos.github.io/icassp23/主要贡献:1)定义了一个新的任务,称为timbre-controlledvideo-to-audi......
  • ShowPrintlnOverload
    packagecom.shrimpking.t6;/***CreatedbyIntelliJIDEA.**@Author:Shrimpking*@create2024/9/1612:14*/publicclassShowPrintlnOverload{publicstaticvoidmain(String[]args){System.out.println(123);//整型System......
  • Python可视化过程中.pictures.add这里一直报错,不明原因
    大家好,我是Python进阶者。一、前言前几天在Python白银交流群【沐子山树】问了一个Python可视化的问题,问题如下:importmatplotlib.pyplotasplt#创建一个简单的图表fig,ax=plt.subplots()ax.plot([1,2,3,4])ax.set_ylabel('somenumbers')#保存图表为PNG文件temp......