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

    }
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
rockrule
  • 83
  • 4
  • 14
  • Hey rockrule, can you format your code using the code button in the editor please. – Greg B Oct 06 '09 at 07:49
  • @rockrule, providing code is fine, but could you please also phrase a question? what do you want to do exactly? – Paolo Tedesco Oct 06 '09 at 08: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 06 '09 at 08:02
  • 2
    If my code solves your problem, then please mark it as the accepted answer :) – Paolo Tedesco Oct 06 '09 at 11:21

1 Answers1

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

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • 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 06 '09 at 08: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. – MusiGenesis Oct 06 '09 at 08: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! – Paolo Tedesco Oct 06 '09 at 09: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 06 '09 at 10:00
  • 1
    @rockrule: adjusted the rectangle, check my code. Now you should see all borders. – Paolo Tedesco Oct 06 '09 at 10:34
  • orsogufo,thanks dude,your code is cool..this solves my problem.thanks very much. – rockrule Oct 06 '09 at 10:55
  • @orsogufo: you need to use the line `Bitmap img = (Bitmap)Bitmap.FromFile(file.FullName);`. Your modified code was the same except it started `Image img = ...`, which meant you were retrieving the file as an Image instead of as a Bitmap (and hence the Graphics error). – MusiGenesis Oct 06 '09 at 12:54
  • Also, I think you only need to subtract 1 from the Width and Height of the Bitmap in order to see all 4 sides of the drawn Rectangle. – MusiGenesis Oct 06 '09 at 12:56
  • Also also, you should call Dispose() on each Graphics object, or place it in a using(){} block. – MusiGenesis Oct 06 '09 at 12:58
  • @MusiGenesis: thanks for your comments, I updated the post with a using statement. The '2' comes from experimenting, 1 is not enough (it had been my first attempt, though, as it seems logical). – Paolo Tedesco Oct 06 '09 at 13:40