0

I need to get selected items from a ListView and copy them to a folder.

I select folder with images and then show them in the ListView. Now I need to get all the selected items and copy them into another selected folder. Is there a way how to do it with better control then listview ?

I tried a few methods, but nothing works.

1 Answers1

0

One approach:

int counter = 1;
        foreach (ListViewItem item in listView1.SelectedItems)
        {
            var image = item.ImageList.Images[item.ImageKey]; //or imageIndex
            using (MemoryStream memory = new MemoryStream())
            {
                using (FileStream fs = new FileStream($@"C:\temp\outputFileName_{counter}", FileMode.Create, FileAccess.ReadWrite))
                {
                    image?.Save(memory, ImageFormat.Jpeg);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
            counter++;
        }
Sevo
  • 22
  • 5
  • ImageKey doesn't work and imageindex return System.Runtime.InteropServices.ExternalException – Dominik Kolouch Jan 20 '20 at 22:07
  • You are right! I've updated my answer! https://stackoverflow.com/a/15862892/4440491 – Sevo Jan 20 '20 at 22:25
  • I have still problems, Authorization.expection And how will it work for more Images? ggg,jpeg will be name just for 1st image FileMode.Create will delete old ggg.jpeg and create new with new image right ? – Dominik Kolouch Jan 20 '20 at 22:34
  • AuthenticationException - I'm guessing you will need to handle your saving credentials. And for saving file names you could use some kind of "template": savedImage_01.jpeg, savedImage_02.jpeg.... so with one image count variable. – Sevo Jan 20 '20 at 22:40
  • Ok, authorization expectation isn't problem... But when I look at the image in the final folder, file have 0 bytes. – Dominik Kolouch Jan 20 '20 at 22:51
  • Do you have ImageList binded to the listview (Small/Large ImageList)? – Sevo Jan 20 '20 at 22:59
  • I'm not sure what you mean. should be this: `for (int j = 0; j < imageList1.Images.Count; j++) { ListViewItem item = new ListViewItem(); item.ImageIndex = j; imageList1.ImageSize = new Size(128, 128); listView1.LargeImageList = imageList1; listView1.Items.Add(item); }` – Dominik Kolouch Jan 20 '20 at 23:04
  • Hmmm, yes, something like that. Then I would suggest to go through the code, line by line, with debugger. Have a look what your image object (in my code) actually is. I'm suspecting it is null. – Sevo Jan 21 '20 at 06:09
  • Yes after, var image = item.ImageList.Images[item.ImageKey]; image is null and fs.Write(bytes, 0, bytes.Length); Bytes[] is 0 and then leght is 0 too. – Dominik Kolouch Jan 21 '20 at 06:54
  • Ok, with imageindex it works. What to do with overiding in final folder ? i can give them index (i for example) and increase it, but every time i will start prog. again, images will override again. Thank you for help – Dominik Kolouch Jan 21 '20 at 07:10
  • Use timestamp instead of index if you don't want to override them on every "export". – Sevo Jan 21 '20 at 11:34