views:

262

answers:

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

    }
+1  A: 

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
hi orsogufu,thank you,the given code does not have any effect on the image.Do I have to set property?.pls help
rockrule
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
@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
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
@rockrule: adjusted the rectangle, check my code. Now you should see all borders.
Paolo Tedesco
orsogufo,thanks dude,your code is cool..this solves my problem.thanks very much.
rockrule
@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
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
Also also, you should call Dispose() on each Graphics object, or place it in a using(){} block.
MusiGenesis
@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