练习:WinForm (PictureBox和Timer)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PictureBox和Timer练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置图片显示样式:平铺
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox6.SizeMode = PictureBoxSizeMode.StretchImage;

            //在窗体加载的时候,给每一个PictureBox都赋值一张图片的路径
            pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\新建文件夹\2.jpg");
            pictureBox2.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\新建文件夹\2.jpg");
            pictureBox3.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\新建文件夹\2.jpg");
            pictureBox4.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\新建文件夹\2.jpg");
            pictureBox5.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\新建文件夹\2.jpg");
            pictureBox6.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\新建文件夹\2.jpg");
        }

        string[] path = System.IO.Directory.GetFiles(@"C:\Users\Administrator\Desktop\新建文件夹");
        Random r = new Random();

        private void timer1_Tick(object sender, EventArgs e)
        {
            //每隔一秒钟换一张图片
            pictureBox1.Image = Image.FromFile(path[r.Next(0,path.Length)]);
            pictureBox2.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox3.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox4.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox5.Image = Image.FromFile(path[r.Next(0, path.Length)]);
            pictureBox6.Image = Image.FromFile(path[r.Next(0, path.Length)]);
        }
    }
}

posted @ 2016-07-01 20:30  庚xiao午  阅读(1691)  评论(0编辑  收藏  举报