7 Replies - 8064 Views - Last Post: 19 March 2011 - 08:59 AM Rate Topic: -----

#1 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 975
  • Joined: 09-May 09

copy image to another directory

Posted 18 March 2011 - 03:06 PM

Hi guys ive been have a bit of a problem with copying a file to another directory.

In my app i display an image, i provide an browse for a new picture button.
This feature will copy the file from the users choosen directory and copy it and place
it into a specific directory.

The problem imhaving is the image i want to replace is in use by the app and wont change.
i use picturebox.dispose() to free up the image in memory but it doesnt seem to work.

 'Completed
    Sub SaveImage(ByVal movieDirectory, ByVal imageType)

        Dim fileDlg As New System.Windows.Forms.OpenFileDialog
        Dim filePath As String

        fileDlg.Title = "Please Select Poster Location"
        fileDlg.InitialDirectory = "C\Pictures:"

        fileDlg.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg"
        If fileDlg.ShowDialog() = DialogResult.OK Then

            'Get seleted file path
            filePath = fileDlg.FileName

            If IO.File.Exists(filePath) Then

                'Change name 
                Dim posterName() As String
                posterName = filePath.Split("\")

                'Create new file path
                Dim newPosterPath = String.Concat(movieDirectory, "\frontCover.jpg")
                Dim newBackdropPath = String.Concat(movieDirectory, "\Bkdrop.jpg")

                'Move and rename file
                If imageType = "poster" Then

                    'free up image
                    Form1.posterPictureBox.Image.Dispose()
                    'Copy image
                    System.IO.File.Copy(filePath, newPosterPath, True)
                Else
                    'free up image
                    Form1.backdropPictureBox.Image.Dispose()
                    'Copy image
                    System.IO.File.Copy(filePath, newBackdropPath, True)
                End If


                'Reload image
                FindImages(movieDirectory)

            End If

        End If

        fileDlg.Dispose()

    End Sub



Any help world be great :)

Is This A Good Question/Topic? 0
  • +

Replies To: copy image to another directory

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: copy image to another directory

Posted 18 March 2011 - 03:24 PM

First point stop using string concatenation for file paths.

Use
System.IO.Path.Combine(ParentPath, ChildPath)

Instead.

Secondly what do you mean by

Quote

it doesnt seem to work.

Is there an error thrown? If so what is it?
Etc. Etc.
Was This Post Helpful? 0
  • +
  • -

#3 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 975
  • Joined: 09-May 09

Re: copy image to another directory

Posted 18 March 2011 - 03:30 PM

System.IO.Path.Combine(ParentPath, ChildPath)

this is a better way, why?

Error Msg:
The process cannot access the file 'E:\Movies\2012\frontCover.jpg' because it is being used by another process.

I thought the .dispose() method would free it from memory.

Kevin
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: copy image to another directory

Posted 18 March 2011 - 03:35 PM

System.IO.Path.Combine(ParentPath, ChildPath)
Is better for several reasons.
1. the underlying file system is not just restricted to just using \ and / to indication folder structure.
2. Not all string characters are allowed in file paths. (It will throw a System.ArgumentException, if it does.)

This post has been edited by AdamSpeight2008: 18 March 2011 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 975
  • Joined: 09-May 09

Re: copy image to another directory

Posted 18 March 2011 - 03:49 PM

Thats good to know, i will start implementing this method from now on. I have a few problems before with special characters.

Do u have any input regardsing the topic?

does the .dispose method free the resource from the memory? should i be doing something different?

Kevin
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: copy image to another directory

Posted 18 March 2011 - 04:18 PM

The process cannot access the file 'E:\Movies\2012\frontCover.jpg' because it is being used by another process.

What else are doing to the file?
Was This Post Helpful? 0
  • +
  • -

#7 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 975
  • Joined: 09-May 09

Re: copy image to another directory

Posted 19 March 2011 - 08:18 AM

Im doing nothing to it. Im displaying the image, i then have a button that opens a dialog and allows the user to select the image they want to display.


Kevin

This post has been edited by deery5000: 19 March 2011 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#8 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: copy image to another directory

Posted 19 March 2011 - 08:59 AM

Use Imports System.IO at the very top of the code file, so you wont have to keep qualifying the namespace (ie, you can just do File.Copy).

You don't want to Dispose a PictureBox.Image, I am pretty sure that will at the very least cause problems in the future when you try to assign another image to the PictureBox.

Where does the image come from in the first place? I know it is on your hard drive, but how to you assign the image to the PictureBox in the first place? Do you create a new Image/Bitmap variable and assign that to PictureBox.Image, or do you use PictureBox.ImageLocation and assign via the path only?
Either way once you dispose of a resource you're telling the Runtime/GC that you're DONE with it, completely. You might try setting the Image instead to Nothing, so that it wont be claimed by the GC.

There are also other methods to save a copy of an Image besides using File.Copy... each Image object has a Save() method which allows you to save the image to a file or stream.
        Dim fPathInit$ = "C:\Users\G"
        Dim fileName$ = "earth_west.jpg"
        Dim fullPath$ = Path.Combine(fPathInit, fileName)
        Dim theImage As New Bitmap(fullPath)            'To hold our image

        pBox.Image = theImage
        pBox.Image.Save("C:\Users\G\earthCopy.jpg")     'Either of the following should work
        theImage.Save("C:\Users\G\earthCopy.jpg")



I have to run, unfortunately, but I'll check in on this thread later. I don't know if it is worth mentioning that the Image classes are reference types (also not thread safe), or if that has anything to do with your particular issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1