Chapter 3. WinForm(PictureBox控件)

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;
using System.IO;

namespace 图片控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //设置图片显示方式:平铺
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            
            //获得指定一种图片的路径
            pictureBox1.Image = Image.FromFile(@"C:UsersAdministratorDesktop新建文件夹1.jpg");
        }

        int i = 0;

        //获得指定文件夹中的所有文件路径
        string[] path = Directory.GetFiles(@"C:UsersAdministratorDesktop新建文件夹");

        /// <summary>
        /// 点击更换下一张图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            i++;
            if (i == path.Length)
            {
                i = 0;
            }
            pictureBox1.Image = Image.FromFile(path[i]);
        }

        /// <summary>
        /// 点击更换上一张图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            i--;
            if(i<0)
            {
                i = path.Length - 1;
            }
            pictureBox1.Image = Image.FromFile(path[i]);
        }
    }
}

原文地址:https://www.cnblogs.com/xiao55/p/5630398.html