wpf中轮询显示图片

本文的需求是,在一个文件夹中,放一堆图片的集合,然后在wpf程序中,按照定时的方式,循序显示照片。

全部代码

1.声明一个PictureInfo类

cs 复制代码
namespace WpfApp1
{
    public class PictureInfo
    {
        public string? FileName { get; set; }
        public string? FilePath { get; set; }
    }
}
  1. 前端界面
cs 复制代码
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Button Content="开始" Click="Button_Click" />
        <UniformGrid Grid.Row="2" Rows="2" Columns="5" Background="Beige"   >
            <!--<TextBlock Text="123"/>
     <Image Source="/imgs/1.jpg"   Height="180" />-->
            <StackPanel Margin="5,0,0,0"  >
                <TextBlock x:Name="txt1"  />
                <Image x:Name="img1"    Height="180" />
            </StackPanel>
            <StackPanel Margin="5,0,0,0">
                <TextBlock x:Name="txt2"  />
                <Image  x:Name="img2"    Height="180" />
            </StackPanel>
            <StackPanel Margin="5,0,0,0">
                <TextBlock  x:Name="txt3" />
                <Image   x:Name="img3"    Height="180" />
            </StackPanel>
            <StackPanel  Margin="5,0,5,0">
                <TextBlock x:Name="txt4" />
                <Image  x:Name="img4"    Height="180" />
            </StackPanel>
            <StackPanel  Margin="5,0,0,0">
                <TextBlock x:Name="txt5" />
                <Image x:Name="img5"     Height="180" />
            </StackPanel>
            <StackPanel  Margin="5,0,0,0">
                <TextBlock x:Name="txt6" />
                <Image   x:Name="img6"    Height="180" />
            </StackPanel>
            <StackPanel Margin="5,0,0,0" >
                <TextBlock x:Name="txt7" />
                <Image x:Name="img7"    Height="180" />
            </StackPanel>
            <StackPanel  Margin="5,0,5,0">
                <TextBlock x:Name="txt8" />
                <Image x:Name="img8"     Height="180" />
            </StackPanel>
            <StackPanel Margin="5,0,0,0" >
                <TextBlock x:Name="txt9" />
                <Image x:Name="img9"    Height="180" />
            </StackPanel>
            <StackPanel  Margin="5,0,5,0">
                <TextBlock x:Name="txt10" />
                <Image x:Name="img10"     Height="180" />
            </StackPanel>
        </UniformGrid>
    </StackPanel>
</Window>

3.后端代码

此处的fileNames可以换做内存读取图片的集合

补充:在内存中转化图片的时候,有很多格式,其中

jpg,格式小

bmp,格式大

png,格式适中

cs 复制代码
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 计时器
        /// </summary>
        DispatcherTimer timer = new DispatcherTimer();

        //获取图片的数据
        string[] fileNames = null;
        public MainWindow()
        {
            InitializeComponent();

            timer.Interval = new TimeSpan(0, 0, 2);//时 分 秒
            timer.Tick += new EventHandler(timer_Tick);

            fileNames = Directory.GetFiles(@"C:\\Users\\60287\\Desktop\\Excel\\3", "*.jpg");
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            timer.Start();
        }
        /// <summary>
        /// 计时器事件
        /// </summary>
        int count = 0;
        private void timer_Tick(object sender, EventArgs e)
        {
            if (count == 12)//12组照片,每组10张照片
            {
                count = 0;//播放完毕,重新开始
            }
            List<PictureInfo> lstPics = new List<PictureInfo>();
            var data = fileNames.Skip(10 * count).Take(10);
            count++;
            for (int i = 0; i < data.Count(); i++)
            {
                PictureInfo pic = new PictureInfo();
                pic.FileName = "Picture" + i.ToString();
                pic.FilePath = data.ToArray()[i].ToString();
                lstPics.Add(pic);
            }
            txt1.Text = lstPics[0].FileName;
            img1.Source = new BitmapImage(new Uri(lstPics[0].FilePath));

            txt2.Text = lstPics[1].FileName;
            img2.Source = new BitmapImage(new Uri(lstPics[1].FilePath));

            txt3.Text = lstPics[2].FileName;
            img3.Source = new BitmapImage(new Uri(lstPics[2].FilePath));

            txt4.Text = lstPics[3].FileName;
            img4.Source = new BitmapImage(new Uri(lstPics[3].FilePath));

            txt5.Text = lstPics[4].FileName;
            img5.Source = new BitmapImage(new Uri(lstPics[4].FilePath));

            txt6.Text = lstPics[5].FileName;
            img6.Source = new BitmapImage(new Uri(lstPics[5].FilePath));

            txt7.Text = lstPics[6].FileName;
            img7.Source = new BitmapImage(new Uri(lstPics[6].FilePath));

            txt8.Text = lstPics[7].FileName;
            img8.Source = new BitmapImage(new Uri(lstPics[7].FilePath));

            txt9.Text = lstPics[8].FileName;
            img9.Source = new BitmapImage(new Uri(lstPics[8].FilePath));

            txt10.Text = lstPics[9].FileName;
            img10.Source = new BitmapImage(new Uri(lstPics[9].FilePath));
        }
    }
}

4.运行效果

本文来源:

wpf中轮询显示图片-CSDN博客

源码地址:

https://download.csdn.net/download/u012563853/89580787

相关推荐
somethingGoWay4 小时前
wpf 只能输入int类型的文本框
wpf
主宰者4 小时前
WPF外部打开html文件
前端·html·wpf
code bean4 小时前
【wpf】WPF 自定义控件绑定数据对象的最佳实践
wpf
code bean4 小时前
【wpf】WPF开发避坑指南:单例模式中依赖注入导致XAML设计器崩溃的解决方案
单例模式·wpf
somethingGoWay4 小时前
wpf 自定义控件,只能输入小数点,并且能控制小数点位数
wpf
我要打打代码10 小时前
wpf触发器
java·linux·wpf
The Sheep 202312 小时前
WPF里的几何图形Path绘制
wpf
somethingGoWay13 小时前
wpf 自定义密码文本框,并且可以双向绑定
wpf
玉面小君1 天前
从 WPF 到 Avalonia 的迁移系列实战篇4:控件模板与 TemplatedControl
wpf
何双新1 天前
第 2 讲:Kafka Topic 与 Partition 基础
kafka·wpf·linq