Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I made one image gallery using ImageList and listView.
Problem is i can load only folder or images once when button click, unable to insert new image, kindly help me to inset new image for existing ImageList.

OR any alternate solution also fine for me

I am using visual studio 2010 and net 4.0

What I have tried:

C#
try
    {
        listView1.Items.Clear();
        DirectoryInfo ds = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\" + "temp" + "\\");
        string importpaths = AppDomain.CurrentDomain.BaseDirectory + "\\" + "temp" + "\\";
        FileInfo[] files = ds.GetFiles();
        ImageList imageList = new ImageList();
        imageList.ColorDepth = ColorDepth.Depth32Bit;
        listView1.View = View.LargeIcon;
        imageList.ImageSize = new Size(200, 200);
        foreach (FileInfo f in files)
        {
            string importpath = importpaths + f.Name;
            string filnames = System.IO.Path.GetFileNameWithoutExtension(importpaths + f.Name);

            System.Drawing.Image _img = System.Drawing.Image.FromFile(importpath);
            Bitmap pic = new Bitmap(300, 300);
            using (Graphics g = Graphics.FromImage(pic))
            {
                g.DrawImage(_img, new System.Drawing.Rectangle(0, 0, pic.Width, pic.Height)); //redraw smaller image
            }

            imageList.Images.Add(filnames, pic);
            
            _img.Dispose();
        }
        for (var i = 0; i < imageList.Images.Count; i++)
        {
            ListViewItem itm = new ListViewItem { ImageIndex = i, Text = imageList.Images.Keys[i] };
           
            listView1.LargeImageList = imageList;
            listView1.Items.Add(itm);             
        }
    }
    catch
    {
        MessageBox.Show("Please Check Temp Folder.");
    }
}
Posted
Updated 23-Aug-20 16:45pm
v3

High level, when you insert image at runtime (using an event - lets say browse an image), you would need to add a new item to the list there.

Very raw level for your understanding, you need to do:
listView1.Items.Add(imageBrowsedObject, listView1.Items.Count+1)


You can always go ahead and modify/add the imagelist and then update your listview with it. Try out.
 
Share this answer
 
Comments
Member 13384819 23-Aug-20 8:18am    
Thank you Sandeep, i have tried for new button event, i unable do it due to less knowledge about this, kindly share some code for insert button....
Member 13384819 23-Aug-20 9:06am    
string importpaths = @" E:\Scanned Images\22082020\2.jpg";

ImageList imageList = new ImageList();
imageList.ColorDepth = ColorDepth.Depth32Bit;
listView1.View = View.LargeIcon;
imageList.ImageSize = new Size(200, 200);

string importpath = importpaths ;
string filnames = System.IO.Path.GetFileNameWithoutExtension(importpaths );

imageList.Images.Add (System.Drawing.Image.FromFile(importpath));
listView1.LargeImageList = imageList;

listView1.Items.Add("test", listView1.Items.Count + 1);

I have tried above the code for button, it will insert but it will remove the existing images from imagelist
This is written quickly, and untested, but I hope it will show you the required steps to add an image to an existing ListViewItem:
public void AddImage(ListViewItem lvitm, ImageList il, string filenm)
{
    Image newImg = Image.FromFile(filenm ?? throw new ArgumentNullException(nameof(filenm)));

    il.Images.Add(newImg);

    int ndx = il.Images.Count - 1;

    lvitm.ImageIndex = ndx;
}
Note what is assumed here:

1 you've already created the new ListViewItem ... that you pass in as a parameter ...and added it to the ListView.

2 the ImageList passed in is already bound to the ListView
 
Share this answer
 
Comments
Member 13384819 24-Aug-20 7:12am    
Thanks for sharing your knowledge, i have one button event which load images from system to Listview using imagelist(working fine). my problem is i need to inset new image for same listviw while run time..
BillWoodruff 24-Aug-20 18:28pm    
Why can't you use the steps shown above to insert a new image at run-time ?
Member 13384819 26-Aug-20 1:45am    
Above steps not working..
BillWoodruff 26-Aug-20 5:40am    
Describe in detail any error messages. What doesn't work ?
Member 13384819 27-Aug-20 2:16am    
when i inset new image , images list will be blank in listview which already are there...

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