出处:http://blog.sina.com.cn/s/blog_60d576800100tf61.html
1 private void PhotoForm_Load(object sender, System.EventArgs e)
2 {
3 this.LoadPhoto();
4 }
5 private void LoadPhoto()
6 {
7 int i= 0;
8 string url = this.GetUrl();
9 string[] sFiles = System.IO.Directory.GetFiles(url+"\images\small\","*.*");
10 for(i=0;i<sFiles.Length;i++)
11 {
12 this.ViewPhoto(i,sFiles[i].ToString());
13 }
14 }
15 /// <summary>
16 /// 取项目路径
17 /// </summary>
18 /// <returns></returns>
19 private string GetUrl()
20 {
21 string b = Application.StartupPath;
22 int i = b.LastIndexOf("\");
23 b = b.Substring(0,i);
24 int j = b.LastIndexOf("\");
25 b = b.Substring(0,j+1);
26 return b;
27 }
28 /// <summary>
29 /// 加载图片控件
30 /// </summary>
31 /// <param name="i">图片序号</param>
32 /// <param name="filePath">文件名</param>
33 private void ViewPhoto(int i,string filePath)
34 {
35 string name="";
36 name = "image_"+i.ToString();
37 PictureBox pb = new PictureBox();
38 pb.Name = name;
39 pb.Image=System.Drawing.Image.FromFile(filePath);
40 //保存文件名
41 pb.Tag = filePath;
42 this.Controls.Add(pb);
43 pb.Width=128;
44 pb.Height =96;
45 pb.Location = new Point(x,y);
46 x=x+230;
47 if((i+1)%4==0 && i!=0)
48 {
49 x=100;
50 y=y+120;
51 }
52 //添加事件
53 pb.Click += new System.EventHandler(picture_Click);
54
55 }
56 private void picture_Click(object sender, System.EventArgs e)
57 {
58 PictureBox pb = (PictureBox)sender;
59 string name = pb.Name;
60 string path = pb.Tag.ToString();
61 }