Draw border for image in listview

前端 未结 1 1506
难免孤独
难免孤独 2020-12-22 01:05

On a treeview after the select event, I populate a listview with images.
I want to custom format these images and place a black color border around each image.



        
相关标签:
1条回答
  • 2020-12-22 01:54

    I would add a border using a Graphics object immediately after loading the images from file:

    EDIT: modified the code, this works for me...

        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);
    

    NOTE: the image copying is intended; if I modify the code to

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

    as suggested in the comments, I get an exception saying "A Graphics object cannot be created from an image that has an indexed pixel format."

    0 讨论(0)
提交回复
热议问题