3 Replies - 757 Views - Last Post: 29 August 2013 - 06:39 AM Rate Topic: -----

#1 Codegenre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-August 13

How to Save All item in Listview in just a sible click of Button?

Posted 28 August 2013 - 07:49 PM

Hi there Everyone!,

I created a system that can manipulate image properties thru listview, i mean all the item in listview will save in properties of the image. Example. (latitude and Longitude).. If i click one item in listview the data are place in textboxes. and when i click save button it will save and clone the image and the image has new properties like Exifdata.(latitude and longitude).

Now my problem is if i select all the items in listview and then click save, only one image are being saved and the image save are duplicating as the number of items in listview.

My question is how to save those items i select or if i select all the image and click save all the images will be save automatically and create a clone one by one.

Here's my code for saving the items..
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnsaveall.Click
        Dim Update As Boolean = False
        
        'line to save the specific file
        If EW.Copyright <> txtcopyright.Text Then
            EW.Copyright = txtcopyright.Text
            Update = True
        End If
        If EW.Description <> txtdescription.Text Then
            EW.Description = txtdescription.Text
            Update = True
        End If
        EW.GPSLatitude = lat
        Update = True
        EW.GPSLongitude = Lon

        Update = True

        EW.GPSLatitudeRef = LatRef
        EW.GPSLongitudeRef = LonRef
        If Update Then
           
            'Get the updated image
            Dim newImage As Bitmap = EW.GetBitmap 
            Dim Fi As IO.FileInfo = New IO.FileInfo(LV.SelectedItems(0).Text) 
            'Calculate a new filename
            Dim NewFileName As String
            Dim nextitem As Integer
            nextitem = LV.Items(0 + 1).Selected = True
            For nextitem = 0 To LV.Items.Count


                LV.Items(0).Selected += 1

                Dim I As Integer = 0
                Do
                    I += 1
                    NewFileName = txtpath.Text & " (" & I.ToString & ")" 
                    txtpath.Text = NewFileName
                    NewFileName = IO.Path.Combine(Fi.DirectoryName, NewFileName & Fi.Extension) ' Fi.DirectoryName = savefiledialog
                Loop Until Not IO.File.Exists(NewFileName)
                'Save the updated image file.
                Select Case Fi.Extension.ToUpper
                    Case ".PNG"
                        newImage.Save(NewFileName, Drawing.Imaging.ImageFormat.Png)
                    Case ".JPG"
                        ' JPG requires a trick for lossless saving
                        Dim NewFilenameTemp = NewFileName & ".temp"
                        ' We rotate the image for 90 degrees aand save to temp 
                        Dim Enc As Encoder = Encoder.Transformation
                        Dim EncParm As EncoderParameter
                        Dim EncParms As New EncoderParameters(1)
                        EncParm = New EncoderParameter(Enc, CLng(EncoderValue.TransformRotate90))
                        EncParms.Param(0) = EncParm
                        Dim CodecInfo As ImageCodecInfo = GetEncoderInfo("image/jpeg")
                        ' Write the rotated image with new properties
                        newImage.Save(NewFilenameTemp, CodecInfo, EncParms)

                        ' Release memory
                        newImage.Dispose()
                        newImage = Nothing
                        GC.Collect()

                        ' Delete the original file
                        System.IO.File.Delete(NewFileName)

                        ' Rotate back and save as new
                        newImage = Image.FromFile(NewFilenameTemp)
                        EncParm = New EncoderParameter(Enc, CLng(EncoderValue.TransformRotate270))
                        EncParms.Param(0) = EncParm
                        newImage.Save(NewFileName, CodecInfo, EncParms)

                        ' Release memory
                        newImage.Dispose()
                        newImage = Nothing
                        GC.Collect()

                        ' Delete the temporary file
                        System.IO.File.Delete(NewFilenameTemp)
                    Case Else
                        MsgBox("Can't save file types " & Fi.Extension)
                End Select
            Next
            End If


Hope someone can help me with this.. or giving idea on how to solve.. any reply are greatly appreciated.. Thanks in Advanced

By the way, i am Jonel from Philippines.. :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to Save All item in Listview in just a sible click of Button?

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to Save All item in Listview in just a sible click of Button?

Posted 28 August 2013 - 10:09 PM

You are not selecting each item in turn. A few comments on the code...

When you reach line 21, Update will always be True because you set it to True in line 17.

I have no idea what sort of code the compiler will generate for the following line:

nextitem = LV.Items(0 + 1).Selected = True

First, nextitem is an integer, and LV.Items(0+1) is a Boolean.
Second, LV.Items(0+1).Selected will cause Item 1 to be selected (0+1 = 1)

You can throw that line away, because you do what's needed later

            For nextitem = 0 To LV.Items.Count

The above line will count nextitem to 1 item past the contents of the ListView.Items array, so it should be:

            For nextitem = 0 To LV.Items.Count -1

In the next line, you are trying to add an integer to a Boolean. A Boolean has only two possible values: True and False.

                LV.Items(0).Selected += 1

So it should be:
                LV.Items(nextitem).Selected = True

I don't use ListView much, so I don't know what other problems you will run into..

One thing, though. Many errors are pointed out to you if you turn Option Strict On. See the link in my signature to find out why it's a great ide.
Was This Post Helpful? 0
  • +
  • -

#3 Codegenre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-August 13

Re: How to Save All item in Listview in just a sible click of Button?

Posted 29 August 2013 - 12:03 AM

Hi thank for reply,

Thanks for correcting my code..

and now my problem was in saving the file.
For nextitem = 0 To LV.Items.Count - 1
                LV.Items(nextitem).Selected = True
                Dim I As Integer = 0
                Do
                    I += 1
                    'NewFileName = txtpath.Text & " (" & I.ToString & ")" 
                    NewFileName = txtpath.Text & " (" & I.ToString & ")"
                    txtpath.Text = NewFileName
                    NewFileName = IO.Path.Combine(Fi.DirectoryName, NewFileName & Fi.Extension)               
 Loop Until Not IO.File.Exists(NewFileName)


it now read the next item in listview but the newfilename still the current item in listview. so that after the looping stage, when i see the folder where the image are being saved. Only one image clone but duplicated by the number of item in listview..

if you have idea for this please let me know..
thanks
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to Save All item in Listview in just a sible click of Button?

Posted 29 August 2013 - 06:39 AM

Comments in the code...

For nextitem = 0 To LV.Items.Count - 1
                LV.Items(nextitem).Selected = True
'  You've selected each ListView item in turn, but you never do anything with them.

                Dim I As Integer = 0
                Do
                    I += 1
                    NewFileName = txtpath.Text & " (" & I.ToString & ")"
'  Here, you've made a filename, but you never change txtpath to the next ListView item
'  txtPath will contain NewFileName on the first time through, and you will just keep
'  putting more numbers on the end of it.

                    txtpath.Text = NewFileName
                    NewFileName = IO.Path.Combine(Fi.DirectoryName, NewFileName & Fi.Extension)               
 Loop Until Not IO.File.Exists(NewFileName)


Try setting a breakpoint on the first line of this code. Run it, and when it stops, single-step through the loop, watching what happens with txtpath.Text, NewFileName, etc. It should become obvious what you need to do.

If you are unsure of how to do this, have a look at one of our debugging turtorials, here and here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1