Question selecting multiple images for conversion (out of listview)

fisherboy998

New member
Joined
May 17, 2013
Messages
4
Programming Experience
Beginner
Ok,
I'm writing a app that should convert a list of images to own size (x/y) and format
The code works, i can preview, convert the image and change its size (x/y), and output format,

I open the filespath of the images as a string via openfiledialog, and list them in a listview
example: c\users\mick\download\Image001.jpg
Item: c\users\mick\downloads
sub1: \image001
sub2: .jpg

Now my problem is:
Converting 1 image goes well, it works the way i want it, gives the size and output as i like...
Selecting more then 1 image and then convert it, gives me all the same images as top selected
I know the problem is within my coding, but i cant find a solution... can somebody help me ou with a new piece of code?

My problem example:
I select Image001, Image002, Image003, Image004, Image005, and Image006
I convert the selected images, but it wil give me 6x Image001...

the code is below, can somebody please have a look at it?
Thank you...


Code:
'Opening more then 1 file'
Dim ofd As New OpenFileDialog()
ofd.Multiselect = True

If ofd.ShowDialog() = DialogResult.OK Then
For Each file In ofd.FileNames

Dim path As String = System.IO.Path.GetDirectoryName(file)
Dim extension As String = System.IO.Path.GetExtension(file)
Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(file)

Dim lvitem As ListViewItem = ListView1.Items.Add(path)
lvitem.SubItems.Add(filename)
lvitem.SubItems.Add(extension)

Next

End If


'Code for converting and saving'
Dim i As Integer
For Each item As ListViewItem In ListView1.SelectedItems
i = item.Index

'Convert size'
Dim original As Image = Image.FromFile(ListView1.SelectedItems(0).Text & "\" & ListView1.SelectedItems(0).SubItems(1).Text & ListView1.SelectedItems(0).SubItems(2).Text)
Dim newbmp As New Bitmap(original, 1600, 1200)
Dim gp As Graphics = Graphics.FromImage(newbmp)

'Check if the directory exists'
Dim strfilename As String
If Not My.Computer.FileSystem.DirectoryExists("C:\Users\Mick\Desktop\Screenshot") Then
My.Computer.FileSystem.CreateDirectory("C:\Users\Mick\Desktop\Screenshot")
End If

'Count how many files are in the directory'
Dim counter = My.Computer.FileSystem.GetFiles(C:\Users\Mick\Desktop\Screenshot)
Dim intCount As Integer
intCount = CStr(counter.Count)

'Output name and file format'
If ComboBox1.SelectedItem = ".Jpeg" Then
strfilename = "Image" & intCount + 1
newbmp.Save(C:\Users\Mick\Desktop\Screenshot\ & strfilename & .jpeg, System.Drawing.Imaging.ImageFormat.Jpeg)
end if
 
This code is very wrong:
VB.NET:
For Each item As ListViewItem In ListView1.SelectedItems
    i = item.Index

    'Convert size'
    Dim original As Image = Image.FromFile(ListView1.SelectedItems(0).Text & "\" & ListView1.SelectedItems(0).SubItems(1).Text & ListView1.SelectedItems(0).SubItems(2).Text)
The reason you're always getting the first selected item is because you're always getting the first selected item. You're using index 0 in that last line regardless. Now, you might think that the solution is to use 'i' instead, but it is not. Let's say that the user selected just the tenth item. It's Index will be 9 so you would then try to get the item at index 9 in the SelectedItems collection. There is no such item so an exception would be thrown. It would be even worse if there was an item at that index because it would be the wrong item.

You have missed the point of the For Each loop. It is to iterate through the items in a list. Why get the item, get the index of that item and then get the item at that index when that simply gives you the item that you had in the first place? Just use the item and forget about indexes:
VB.NET:
For Each item As ListViewItem In ListView1.SelectedItems
    'Convert size'
    Dim original As Image = Image.FromFile(item.Text & "\" & item.SubItems(1).Text & item.SubItems(2).Text)
 
VB.NET:
For Each item As ListViewItem In ListView1.SelectedItems
    'Convert size'
    Dim original As Image = Image.FromFile(item.Text & "\" & item.SubItems(1).Text & item.SubItems(2).Text)

OMG!, u are my hero!!! :O :O :O!!!!!!
it works perfectly :D!


now i need to figure out using a progressbar around this... :)
i should do something like: progressbar1.value = current item / total items? :O
any tips on programming this? :)
 
Back
Top