当前位置: 首页 > news >正文

【C#】Winform实现轮播图

复制后,需要修改的代码:

1、图片文件夹路劲:string folderPath = "C:\\Users\\Administrator\\Desktop\\images";

2、项目命名空间:namespace BuildAction

全窗口代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;namespace BuildAction
{public partial class Form1 : Form{private string[] imageFiles;       // 存储图片文件路径private int currentIndex = 0;      // 当前显示的图片索引private Thread rotateThread;       // 轮播线程public Form1(){InitializeComponent();}private void StartRotate(){// 读取指定文件夹下的所有图片string folderPath = "C:\\Users\\Administrator\\Desktop\\images";imageFiles = Directory.GetFiles(folderPath, "*.png", SearchOption.TopDirectoryOnly);rotateThread = new Thread(() =>{while (true){// 显示下一张图片currentIndex = (currentIndex + 1) % imageFiles.Length;this.Invoke(new Action(() =>{pictureBox1.Image = Image.FromFile(imageFiles[currentIndex]);}));// 等待 2 秒后切换图片Thread.Sleep(2000);}});rotateThread.IsBackground = true;rotateThread.Start();}private void StopRotate(){if (rotateThread != null && rotateThread.IsAlive){rotateThread.Abort();}}private void Form1_Load(object sender, EventArgs e){// 开始轮播StartRotate();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){StopRotate();}private void button1_Click(object sender, EventArgs e){MessageBox.Show("测试多线程是否堵塞", "提醒");}}
}

http://www.lryc.cn/news/196121.html

相关文章:

  • MyBatisPlus(十九)自动填充
  • 设计模式_命令模式
  • python接口自动化测试(六)-unittest-单个用例管理
  • tomcat 服务器
  • 如果你有一次自驾游的机会,你会如何准备?
  • 关于ts的keyof
  • Go实现CORS(跨域)
  • 第一章:变量和简单的数据类型
  • 【初识Linux】:常见指令(2)
  • “torch.load“中出现的“Unexpected key(s) in state_dict“报错问题
  • 使用dasviewer加载osgb模型,不显示纹理,黑乎乎的怎么解决?
  • Qtday01(qt简介、简单窗口组件)
  • 【SA8295P 源码分析 (一)】41 - SA8295所有镜像位置、拷贝脚本、生成QFIL包 及 Fastboot 下载命令介绍
  • AtCoder abc130
  • 数据库、数据中台、数据仓库、数据湖区别
  • 缺失的数据范围,思维,hduoj
  • 极简的MapReduce实现
  • 更新暑假做过的项目(医学数据多标签分类与多标签分割,医学数据二分类)
  • 谷歌浏览器访问127.0.0.1时报错 Failed to read the ‘sessionStorage‘ property from ‘Window‘
  • 云技术分享 | 快速构建 CodeWhisperer 代码生成服务,让 AI 辅助编程
  • 开发万岳互联网医院APP:技术要点和关键挑战
  • 漫谈下一代防火墙与Web应用防火墙的区别
  • 基于马尔可夫随机场的图像去噪算法matlab仿真
  • 【综合类型第 39 篇】HTTP 状态码详解
  • win10 hosts文件修改不生效
  • 网络库OKHttp(1)流程+拦截器
  • 关于 Invalid bound statement (not found): 错误的解决
  • 深入理解强化学习——智能体的类型:有模型强化学习智能体与免模型强化学习智能体
  • vue项目获得开源代码之后跳过登录界面
  • WPS、Excel表格增加一列,序列1到任意大小 / 填充某个范围的数字到列