重要属性:
pictureBox中SizeMode可以更改图像显示的尺寸大小。
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; namespace ListBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //为了在两个方法中都能访问到 String[] path = Directory.GetFiles("E:\00","*.jpg"); private void Form1_Load(object sender, EventArgs e) { for ( int i = 0; i < path.Length; i++) { //根据路径名获取文件名称 string fileName = Path.GetFileName(path[i]); listBox1.Items.Add(fileName); } } private void listBox1_DoubleClick(object sender, EventArgs e) { //添加图片文件,需要添加全路径 pictureBox1.Image = Image.FromFile(path[listBox1.SelectedIndex]); } } }
使用List
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; namespace ListBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //为了在两个方法中都能访问到 List<string> list = new List<string>(); private void Form1_Load(object sender, EventArgs e) { String[] path = Directory.GetFiles("E:\00", "*.jpg"); for ( int i = 0; i < path.Length; i++) { //根据路径名获取文件名称 string fileName = Path.GetFileName(path[i]); listBox1.Items.Add(fileName); //将图片全路径添加到List泛型中; list.Add(path[i]); } } private void listBox1_DoubleClick(object sender, EventArgs e) { //添加图片文件,需要添加全路径 pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]); } private void listBox1_Click(object sender, EventArgs e) { pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]); } } }