1

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.

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

    }
4
  • Hey rockrule, can you format your code using the code button in the editor please.
    – Greg B
    Oct 6, 2009 at 7:49
  • @rockrule, providing code is fine, but could you please also phrase a question? what do you want to do exactly? Oct 6, 2009 at 8:00
  • On a treeview after select event,i populate listview with images.I want to custom format these image inside listview and place a black color border around each image.Please help
    – rockrule
    Oct 6, 2009 at 8:02
  • 2
    If my code solves your problem, then please mark it as the accepted answer :) Oct 6, 2009 at 11:21

1 Answer 1

1

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."

10
  • hi orsogufu,thank you,the given code does not have any effect on the image.Do I have to set property?.pls help
    – rockrule
    Oct 6, 2009 at 8:17
  • I'd change the line that begins Image img = ... to Bitmap img = (Bitmap)Bitmap.FromFile(file.FullName);. Your version loads the file and then makes a Bitmap copy of it. Oct 6, 2009 at 8:58
  • @MusiGenesis: thanks for your comment, but I'm making a copy on purpose (see edited answer). If you know how to avoid the error without making a copy, please let me know! Oct 6, 2009 at 9:07
  • thank you orsogufo, the code suggested by orsogufo works,but i could see the border on only 2 side of the image.Am I missing something.
    – rockrule
    Oct 6, 2009 at 10:00
  • 1
    @rockrule: adjusted the rectangle, check my code. Now you should see all borders. Oct 6, 2009 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.