Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
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 filepath as 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

Problem is:
Converting 1 image goes well, it works the way i want it
Selecting more then 1 image and then convert it, gives me all the same images as top selected

For 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...



'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
Posted
Comments
Sergey Alexandrovich Kryukov 15-May-13 17:34pm    
Thank you for correct and comprehensive formulation of the problem! I was already started to thing that we don't have inquirer with this basic skill anymore. :-)
—SA

1 solution

In For Each item As ListViewItem In ListView1.SelectedItems your loop variable is item. This is what you should use to extract data, but you are doing something else (I don't want to analyze what exactly, you are not using item, not even i, so it's all wrong anyway).

And there is no a need to get item.ItemIndex, as the current item reference is already available inside the loop.

Fix it and it will work.

—SA
 
Share this answer
 
v2
Comments
fisherboy998 16-May-13 12:41pm    
i know i basicly just have to loop trough every selected item and convert them...
but i'm not really sure how to do it... i thank you for your answer, its clear

but im really just a basic coder...
i dont wanna ask but can u help me out a little? :O
Sergey Alexandrovich Kryukov 16-May-13 14:14pm    
I though you do know. Read what did you right by yourself: "Converting 1 image goes well...".

Your concern was different: you had some bug converting only the first image, not the whole collection of them. I told you where the bug is and how to fix it. What else would you need?

So, please try to fix this bug and accept this answer formally (green button). If you still have some other problem, ask another question. This one is answered.

—SA
fisherboy998 16-May-13 14:44pm    
Dear Sergey,

The problem itself was actually converting more then one image... :O
if i select 6 different images and press convert, i get an output result of 6 times the same image and not 6 different...

code like: for each original as listviewitem in listview1.selecteditem doesn't work :O
as then i have to remake my dim original as, and thats were i get stuck... :/
i can't find a solution to preperly adjust the code...

im not an ICT / computer man... i study animal care taker, and code as hobby,
i started 2 months ago, and i don't really understand alot of it :/

sorry,
Sergey Alexandrovich Kryukov 16-May-13 15:58pm    
Look, I did not explain you that I'm a physicist. How it can be relevant? You either do the job or do not; if you do, you do it in not a less than a professional way, everything else is fair tales...
I explain you then bug, but you did not explain me what's the remaining problem...
—SA
Sergey Alexandrovich Kryukov 16-May-13 16:03pm    
Actually, I cannot see how even one file could be converted. Your code could not compile. For example:

My.Computer.FileSystem.CreateDirectory(C:\Users\Mick\Desktop\Screenshot)

must be

My.Computer.FileSystem.CreateDirectory("C:\Users\Mick\Desktop\Screenshot")

But even this you should not right, as there are no cases where hard-coded file paths could be useful.

But is simply means that you did not provide true information on your problem and show anything but not your real code fragment. I just did not look at this part. How can anyone can help if you do it?

—SA

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