Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I try to fill a listview with imagefiles by an folderBrowserDialog. Who know a fast way to do this?

Thank you :)
Posted
Updated 12-Mar-15 4:24am
v3
Comments
Sinisa Hajnal 12-Mar-15 10:24am    
Easily found online.

First I'm guessing you want to fill out the System.Windows.Forms.ListView, right?
Second I'm guessing you want to fill it with image files located inside a folder which is selected with FolderBrowserDialog, right?
In the future you really should specify your issues better...

Nevertheless what you need to do is fill the ListView's LargeImageList or a SmallImageList (depending on what ListView's View you are going to use) with the Image objects created from image files.
So something like the following:
C#
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
    this.listView1.View = View.LargeIcon;
    this.listView1.LargeImageList = new ImageList() { ImageSize = new Size(64, 64) };

    var imageFiles = from file in Directory.EnumerateFiles(this.folderBrowserDialog1.SelectedPath)
                    let extension = Path.GetExtension(file)
                    // Add more image extensions if needed ... 
                    where extension.Equals(".jpg") || extension.Equals(".png")
                    select file;

    int imageIndex = 0;
    foreach (string imageFile in imageFiles)
    {
        this.listView1.LargeImageList.Images.Add(Image.FromFile(imageFile));
        this.listView1.Items.Add(null, imageIndex++);
    }
}
 
Share this answer
 
Comments
Karsten Klinger 12-Mar-15 14:59pm    
Thank you... That's great :)
So I have a second question. How can I realize a extensionfilter with drag&drop?
Mario Z 12-Mar-15 15:24pm    
I would like to help you but I'm not sure what you mean.
Could you clarify a bit what you would like to achieve?
Do you want to Drag&Drop some Windows Forms controls that would represent different image extensions and then depending on the chosen extensions make the filtering?
To be honest to me this seems like an unintuitive UI, did you consider simply using the CheckBoxes instead?
Nevertheless if you can try to provide some additional information.
Karsten Klinger 12-Mar-15 15:36pm    
I write an application to send pictures to an FUJIFILM minilab.
In the System.Windows.Forms.ListView I "collect" the files for a order.
I want to have three possibilities to fill the Listview.

1st: Whole Pictures of a folder by a folderBrowserDialog
2nd: Single files by a openFileDialog
3rd: By drag and drop into the ListView.

I hope now you know what I mean.
I see now, so you want to Drag & Drop the images onto the ListView control, well that makes sense.
For this you need to enable Drag & Drop feature in your application, here is a short overview of Drag-and-Drop Functionality in Windows Forms.
And here is an example how you can do this:
C#
// I moved this outside.
private int imageIndex = 0;

public Form1()
{
    InitializeComponent();

    this.listView1.AllowDrop = true;

    this.listView1.DragEnter += (sender, e) =>
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
    };

    this.listView1.DragDrop += (sender, e) =>
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

        // You can notice that the following lines are repetition which I also used in above sample code.
        // So you can tweak a bit your code so that you don't repeat yourself.
        var imageFiles = from file in files
                            let extension = Path.GetExtension(file)
                            where extension.Equals(".jpg") || extension.Equals(".png")
                            select file;

        foreach (string imageFile in imageFiles)
        {
            this.listView1.LargeImageList.Images.Add(Image.FromFile(imageFile));
            this.listView1.Items.Add(null, this.imageIndex++);
        }
    };
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900