• winform绑定多张图片


    开发winform程序的时候经常设计到要显示多张图片的问题,其解决思路一般是先遍历文件夹中的所有图片,然后再把这些图片添加到ImageList控件中,最后再绑定显示出来。这里我们介绍两种绑定的方法:

    (一)动态生成PictureBox绑定图片

    (1)先在界面添加ImageList和PictureBox控件

    (2)遍历文件夹中的所有图片,并添加到ImageList中

    (3)根据图片的数量来动态生成PictureBox,并依次绑定显示

     1 public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6             string[] arrFileNames = System.IO.Directory.GetFiles(@"E:五月天Q版五月天");
     7             Image img = null;
     8             foreach (string name in arrFileNames)
     9             {
    10                 img = Image.FromFile(name);
    11                 imageList1.Images.Add(img);
    12             }
    13 
    14             PictureBox pb;
    15             for (int i = 0; i < arrFileNames.Length; i++)
    16             {
    17                 pb = new PictureBox();
    18                 pb.Width = 130;
    19                 pb.Height = 170;
    20                 pb.Image = imageList1.Images[i];
    21                 pb.Location = new System.Drawing.Point(0, i * 160);
    22                 panel1.Controls.Add(pb);
    23             }
    24         }
    25     }
    View Code

    (二)ListView绑定多张图片

    其大概思路同上,不啰嗦了,直接附上代码

     1     public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6             try
     7             {
     8                 List<string> tifNames = new List<string>();
     9                 string path = @"E:五月天Q版五月天";
    10                 DirectoryInfo TheFolder = new DirectoryInfo(path);//文件路径  
    11                 imgListPhoto.Images.Clear();
    12                 for (int i = 0; i < TheFolder.GetFiles().Length; i++)   //遍历文件夹           
    13                 {
    14                     if (TheFolder.GetFiles()[i].Length > 0 && TheFolder.GetFiles()[i].Extension == ".jpg")                //或者jpg,png 文件大小要大于0且是图片文件               
    15                     {
    16                         Image image = Image.FromFile(TheFolder.GetFiles()[i].DirectoryName + "\" + TheFolder.GetFiles()[i].Name);                    //获取文件                     
    17                         tifNames.Add(TheFolder.GetFiles()[i].Name);//添加文件名        
    18                         imgListPhoto.Images.Add(image);//添加图片    
    19                     }
    20                 }
    21                 //初始化设置     
    22                 this.listView1.View = View.LargeIcon;
    23                 this.listView1.LargeImageList = this.imgListPhoto;
    24 
    25                 //开始绑定           
    26                 this.listView1.BeginUpdate();
    27                 this.listView1.Items.Clear();
    28                 ListViewItem items = new ListViewItem();
    29                 items.SubItems.Clear();
    30                 for (int i = 0; i < tifNames.Count; i++)
    31                 {
    32                     ListViewItem lvi = new ListViewItem();
    33                     lvi.ImageIndex = i;
    34                     lvi.Text = tifNames[i];
    35                     this.listView1.Items.Add(lvi);
    36                     Thread.Sleep(200);
    37                 }
    38                 this.listView1.EndUpdate();
    39             }
    40             catch (Exception ex)
    41             {
    42                 //MessageBox.Show("Error");
    43                 throw new Exception(ex.Message);
    44             }
    45         }
    46     }
    View Code

    个人建议还是用ListView,而动态生成控件则可作为一种思路,在其他很多方面都可以用到的

  • 相关阅读:
    常用控件(1)—RadioGroup、CheckBox、Toast
    调用系统剪切板并传输到OtherActivity
    Linux 文件类型
    Handler应用3 — 与Activity不同一线程
    linux下tftp(解决Loading问题 transfer timed out)
    c/c++ 中const的作用
    C++ 初步知识
    类外定义成员函数实例
    ubuntu上Samba服务器配置
    Android全面开发——问题汇总
  • 原文地址:https://www.cnblogs.com/weizhengLoveMayDay/p/3330894.html
Copyright © 2020-2023  润新知