• WPF实现轮播图(图片、视屏)


    在这里插入图片描述

    ✅作者简介:2022年博客新星 第八。热爱国学的Java后端开发者,修心和技术同步精进。
    🍎个人主页:Java Fans的博客
    🍊个人信条:不迁怒,不贰过。小知识,大智慧。
    💞当前专栏:WPF 案例及知识分享专栏
    ✨特色专栏:乐趣国学-心性养成之路
    🥭本文内容:WPF实现轮播图(图片、视屏)

    在这里插入图片描述

    1、WPF技术实现图片轮播

      以下是一个使用WPF技术实现图片轮播的简单案例代码示例。在这个示例中,我们将使用Image控件来显示图片,并使用DispatcherTimer来实现图片切换的定时效果。

      首先,在XAML文件中创建一个窗口,并添加一个Image控件用于显示图片:

    <Window x:Class="ImageSlider.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Image Slider" Height="400" Width="600">
        <Grid>
            <Image Name="imageControl" Stretch="UniformToFill"/>
        Grid>
    Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

      然后,在C#代码中,实现图片轮播逻辑:

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Threading;
    
    namespace ImageSlider
    {
        public partial class MainWindow : Window
        {
            private List<string> imagePaths = new List<string>
            {
                "image1.jpg",
                "image2.jpg",
                "image3.jpg",
                // 添加更多图片路径
            };
    
            private int currentIndex = 0;
            private DispatcherTimer timer = new DispatcherTimer();
    
            public MainWindow()
            {
                InitializeComponent();
                timer.Interval = TimeSpan.FromSeconds(5); // 设置图片切换间隔
                timer.Tick += Timer_Tick;
                LoadImage(currentIndex); // 初始加载第一张图片
                timer.Start(); // 启动定时器
            }
    
            private void Timer_Tick(object sender, EventArgs e)
            {
                currentIndex++;
                if (currentIndex >= imagePaths.Count)
                {
                    currentIndex = 0;
                }
                LoadImage(currentIndex);
            }
    
            private void LoadImage(int index)
            {
                if (index >= 0 && index < imagePaths.Count)
                {
                    string imagePath = imagePaths[index];
                    BitmapImage bitmapImage = new BitmapImage(new Uri(imagePath, UriKind.Relative));
                    imageControl.Source = bitmapImage;
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

      在上述代码中,我们首先定义了一个包含图片路径的列表 imagePaths,然后使用DispatcherTimer来定时切换图片。在窗口初始化时,我们加载第一张图片并启动定时器,定时器触发时会切换到下一张图片。

      请确保将示例代码中的图片路径替换为你自己的图片路径,并根据需要调整定时器的间隔。

    2、WPF技术实现视屏轮播

      要在WPF应用程序中实现视频轮播,你可以使用MediaElement控件来播放视频,并使用DispatcherTimer来控制视频的切换。以下是一个简单的示例代码,演示如何实现视频轮播:

      首先,在XAML文件中创建一个窗口,并添加一个MediaElement控件用于播放视频:

    <Window x:Class="VideoSlider.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Video Slider" Height="400" Width="600">
        <Grid>
            <MediaElement Name="mediaElement" Stretch="Fill" LoadedBehavior="Play" UnloadedBehavior="Stop" />
        Grid>
    Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

      然后,在C#代码中,实现视频轮播逻辑:

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Threading;
    using System.Windows.Media;
    
    namespace VideoSlider
    {
        public partial class MainWindow : Window
        {
            private List<string> videoPaths = new List<string>
            {
                "video1.mp4",
                "video2.mp4",
                "video3.mp4",
                // 添加更多视频路径
            };
    
            private int currentIndex = 0;
            private DispatcherTimer timer = new DispatcherTimer();
    
            public MainWindow()
            {
                InitializeComponent();
                timer.Interval = TimeSpan.FromSeconds(10); // 设置视频切换间隔
                timer.Tick += Timer_Tick;
                LoadVideo(currentIndex); // 初始加载第一个视频
                timer.Start(); // 启动定时器
            }
    
            private void Timer_Tick(object sender, EventArgs e)
            {
                currentIndex++;
                if (currentIndex >= videoPaths.Count)
                {
                    currentIndex = 0;
                }
                LoadVideo(currentIndex);
            }
    
            private void LoadVideo(int index)
            {
                if (index >= 0 && index < videoPaths.Count)
                {
                    string videoPath = videoPaths[index];
                    Uri videoUri = new Uri(videoPath, UriKind.Relative);
                    mediaElement.Source = videoUri;
                    mediaElement.Play();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

      在上述代码中,我们首先定义了一个包含视频文件路径的列表 videoPaths,然后使用DispatcherTimer来定时切换视频。在窗口初始化时,我们加载第一个视频并启动定时器,定时器触发时会切换到下一个视频。

      请确保将示例代码中的视频文件路径替换为你自己的视频文件路径,并根据需要调整定时器的间隔。

    3、WPF技术实现图片视屏组合轮播

      要在WPF应用程序中实现图片和视频的轮播混合效果,可以借助MediaElement控件播放视频,同时使用Image控件来显示图片。以下是一个示例代码,演示如何实现图片和视频的轮播混合效果:

      首先,在XAML文件中创建一个窗口,包含一个MediaElement用于播放视频和一个Image用于显示图片:

    <Window x:Class="MediaSlider.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Media Slider" Height="400" Width="600">
        <Grid>
            <MediaElement Name="mediaElement" Stretch="Fill" LoadedBehavior="Play" UnloadedBehavior="Stop" />
            <Image Name="imageControl" Stretch="UniformToFill"/>
        Grid>
    Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

      然后,在C#代码中,实现图片和视频的轮播逻辑:

    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Media.Imaging;
    using System.Windows.Threading;
    
    namespace MediaSlider
    {
        public partial class MainWindow : Window
        {
            private List<string> mediaPaths = new List<string>
            {
                "video1.mp4",
                "image1.jpg",
                "video2.mp4",
                "image2.jpg",
                // 添加更多视频和图片路径
            };
    
            private int currentIndex = 0;
            private DispatcherTimer timer = new DispatcherTimer();
    
            public MainWindow()
            {
                InitializeComponent();
                timer.Interval = TimeSpan.FromSeconds(10); // 设置切换间隔
                timer.Tick += Timer_Tick;
                LoadMedia(currentIndex); // 初始加载第一个媒体
                timer.Start(); // 启动定时器
            }
    
            private void Timer_Tick(object sender, EventArgs e)
            {
                currentIndex++;
                if (currentIndex >= mediaPaths.Count)
                {
                    currentIndex = 0;
                }
                LoadMedia(currentIndex);
            }
    
            private void LoadMedia(int index)
            {
                if (index >= 0 && index < mediaPaths.Count)
                {
                    string mediaPath = mediaPaths[index];
                    if (mediaPath.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
                    {
                        // 如果是视频文件
                        Uri videoUri = new Uri(mediaPath, UriKind.Relative);
                        mediaElement.Source = videoUri;
                        mediaElement.Play();
                        imageControl.Visibility = Visibility.Collapsed; // 隐藏图片
                        mediaElement.Visibility = Visibility.Visible; // 显示视频
                    }
                    else if (mediaPath.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
                    {
                        // 如果是图片文件
                        BitmapImage bitmapImage = new BitmapImage(new Uri(mediaPath, UriKind.Relative));
                        imageControl.Source = bitmapImage;
                        imageControl.Visibility = Visibility.Visible; // 显示图片
                        mediaElement.Visibility = Visibility.Collapsed; // 隐藏视频
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

      在上述代码中,我们定义了一个包含视频文件和图片文件路径的列表 mediaPaths,并使用DispatcherTimer来定时切换媒体。在窗口初始化时,我们加载第一个媒体(可以是视频或图片),并启动定时器,定时器触发时会切换到下一个媒体。

      根据文件的扩展名来判断是视频还是图片,并相应地设置MediaElement和Image的可见性。

      请确保将示例代码中的媒体文件路径替换为你自己的文件路径,并根据需要调整定时器的间隔。


      码文不易,本篇文章就介绍到这里,如果想要学习更多Java系列知识点击关注博主,博主带你零基础学习Java知识。与此同时,对于日常生活有困扰的朋友,欢迎阅读我的第四栏目《国学周更—心性养成之路》,学习技术的同时,我们也注重了心性的养成。

    在这里插入图片描述

  • 相关阅读:
    LeetCode: 数组峰值与谷值问题总结 - Python
    Rethinking Minimal Sufficient Representation in Contrastive Learning 论文解读和感想
    【Redis】深入探索 Redis 集群(Cluster)模式的概念、原理、数据分片算法,基于 Docker 模拟搭建 Redis 集群分布式架构
    unable to boot the simulator. domain nsposixerrordomain code 4
    吉林教育杂志吉林教育杂志社吉林教育编辑部2022年第28期目录
    Unity接入北斗探针SDK(基于UnityPlayerActivity)丨二、usb-serial-for-android导出jar包
    MySQL安装教程-手把手教你安装
    基于机器视觉的移动消防机器人(三)--软件设计
    Linux- 自定义一个ARP请求
    【three.js】结合vue进行开发第一个3d页面
  • 原文地址:https://blog.csdn.net/hh867308122/article/details/132884216