绘制边界在列表视图图像(Draw border for image in listview)

2019-07-21 01:29发布

在选择事件后一个TreeView,我填充图像列表视图。
我想自定义格式这些图像,并把黑色边框的每个图像周围。

   private void TreeView1_Select(object sender, EventArgs e) {
        if (folder != null && System.IO.Directory.Exists(folder)) {

            DirectoryInfo dir = new DirectoryInfo(@folder);
            foreach (FileInfo file in dir.GetFiles()) {
                try {
                    imageList.Images.Add(Image.FromFile(file.FullName));
                } catch {
                    Console.WriteLine("This is not an image file");
                }
            }

            for (int j = 0; j < imageList.Images.Count; j++) {
                this.ListView1.Items.Add("Item" + j);
                this.ListView1.Items[j].ImageIndex = j;
            }

            this.ListView1.View = View.LargeIcon;
            this.ListView1.LargeImageList = imageList;
            this.ListView1.DrawItem += 
                new DrawListViewItemEventHandler(ListView1_DrawItem);

        }
    }

    private void ListView1_DrawItem(object sender, DrawListViewItemEventArgs e) 
    {

    }

Answer 1:

我想补充使用边框Graphics加载从文件中的图像后立即对象:

编辑 :修改了代码,这个工作对我来说...

    private void TreeView1_Select(object sender, EventArgs e) {
        if (folder != null && System.IO.Directory.Exists(folder)) {

            DirectoryInfo dir = new DirectoryInfo(@folder);
            foreach (FileInfo file in dir.GetFiles()) {

                Image img = new Bitmap(Image.FromFile(file.FullName));
                using (Graphics g = Graphics.FromImage(img)){
                    g.DrawRectangle(Pens.Black, 0, 0, img.Width - 2, img.Height - 2);
                }
                imageList.Images.Add(img);

:图像复制的目的; 如果我修改代码以

    Image img = (Bitmap)Bitmap.FromFile("test.bmp");

作为意见建议,我得到一个异常说“图形对象不能从具有索引像素格式的图像创建的。”



文章来源: Draw border for image in listview