图片自动浏览器
前言
C#实践开发_Winform 系列第四篇:图片自动浏览器,掌握文件对话框(OpenFileDialog)、图片框控件(PictureBox)的使用,PictureBox既可以显示静态图形,也可播放动态图形。一、结果呈现
1. 界面设计
窗体界面设计:一个文件对话框(OpenFileDialog)、一个图片框控件(PictureBox)、两个button按钮、一个Timer定时器、一个列表框。2. 运行结果呈现
二、源码
1.Form.cs
代码如下(示例):
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;
namespace test_4_2
{
public partial class Form3 : Form
{
private int PicNo;
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
PicNo = 0;
timer1.Enabled = false;
openFileDialog1.Multiselect = true;
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "位图文件|*.bmg|GIF文件|*.gif|JPEG文件|*.jpg"; //设置过滤器,只显示图像文件
openFileDialog1.FilterIndex = 3; //指定默认过滤器(默认打开JPEG文件)
openFileDialog1.ShowDialog(); //显示打开对话框
listBox1.Items.Add(openFileDialog1.FileName); //将用户选定文件载入列表框
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Trick(object sender, System.EventArgs e)
{
listBox1.SelectedIndex = PicNo;
string s = listBox1.SelectedItem.ToString(); //得到某一要显示图片的路径
pictureBox1.Image = Image.FromFile(s); //加载图片
PicNo += 1;
if (PicNo >= listBox1.Items.Count) //判断,如果为最后一张,则转为第一张
PicNo = 0;
}
}
}
2.Form.Designer.cs
代码如下(示例):
namespace test_4_2
{