Csharp中listview中自动加载imagelist中的本地图片

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.Data.SqlClient;
namespace imagelist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadImageList();//窗体创建完毕自动加载到ListView
        }
        private void LoadImageList()
        {
            // Environment.CurrentDirectory
            //AppDomain.CurrentDomain.BaseDirectory
            //自动获取图片文件夹路径
            var path = AppDomain.CurrentDomain.BaseDirectory+ "/images/";
            var list = new List<string>();
            //图片命令格式,统一一下,可以是Png,jbg,bmp
            list.Add("01-01-0001.ico");
            list.Add("01-02-0002.ico");
            list.Add("01-03-0003.ico");
            list.Add("01-04-0004.ico");
            list.Add("01-05-0005.ico");
            list.Add("01-06-0006.ico");
            list.Add("01-07-0007.ico");
 
            ImageList imglist = new ImageList();
            imglist.ImageSize = new Size(48, 48);//设置图片显示大小
            imglist.ColorDepth = ColorDepth.Depth32Bit;//设置位图深度
            foreach (var fileName in list)
            {
                imglist.Images.Add(Image.FromFile(path + fileName));
                picListView.View = View.LargeIcon;//设置显示方式,前面设置了大小,可以省略
            }
            picListView.LargeImageList = imglist;
 
            for (int i = 0; i < imglist.Images.Count; i++)
            {
                var lvi = new ListViewItem();
                lvi.ImageIndex = i;
                lvi.Text = "P" + i;
                lvi.ToolTipText = "P" + i;
                picListView.Items.Add(lvi);//添加数据
            }
        }
    }
}

发布日期:

所属分类: 编程, 编程语言 标签:


没有相关文章!