13 Replies - 463 Views - Last Post: 28 December 2017 - 05:27 PM Rate Topic: -----

#1 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Saving image as a pathfile to textfiles

Posted 28 December 2017 - 10:49 AM

I am saving and retreiving from textfiles.

Saving the image as an imagepath and have added it to my user structure

Writing :

User.Imagepath = txt_ImagePath.text


Retreiving :

pbx_UserImage.image = User.Imagepath 


however there is an error (syntax blue line) that value of type string cannot be converted to system.drawing.image and I understand that but not sure how else it can be resolved

Is This A Good Question/Topic? 0
  • +

Replies To: Saving image as a pathfile to textfiles

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13749
  • View blog
  • Posts: 54,904
  • Joined: 12-June 08

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 11:15 AM

I don't get what you are trying to do.

If you want to instantiate an image object from a file location that's one thing, but you can not assign an image object a string.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5143
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 11:17 AM

Well you need to build an image using the image file path string. Check out "Image.FromFile()" function. This function will take in your string path and it will go fetch the image and create an image from it to be used as an Image object. Below is the link to MSDN which shows you a nice little example of how this is done (Be sure to click the VB tab to see the VB version)...

https://msdn.microso...(v=vs.110).aspx

Notice there that they use some image paths to a couple of .jpg files to make the image objects. These can then be set directly into anywhere that needs an image... like a picturebox etc. Hope this is what you were looking for. :)

This post has been edited by Martyr2: 28 December 2017 - 11:20 AM

Was This Post Helpful? 1
  • +
  • -

#4 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 11:47 AM

martyn I am a bit confused, how would i do it :)/>?

I read that link and still a bit confused

Appreciate your lovely support !

This post has been edited by WizWazard: 28 December 2017 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 12:09 PM

I have declare this as a global variable

Dim UserImage As Image


when Im writing to file i have this code

AdminImage = Image.FromFile(Tbx_ImagePath.Text)


when Im retreiving the image i have this code

   Pbx_AdminImage.Image = AdminImage 


but some images dont seem to load but the filepath is stored in the textfile

This post has been edited by WizWazard: 28 December 2017 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5143
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 01:13 PM

The path you are using is stored in a file or is the path you are using the path to the image file? If the path is in a file, you have to first read it out of the file. The main goal is to get a path to the image you are trying to load. So you want a path like c:/my_directory/my_file.jpg or something, not c:/my_directory/sometextfile.txt. Just look at the example page again. Notice that the paths they provide are to the image file.

I am assuming that the string in Tbx_ImagePath.Text is the path to the image file.

Quote

but some images dont seem to load but the filepath is stored in the textfile


Can I assume from the statement above that you are able to load some images?

This post has been edited by Martyr2: 28 December 2017 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

#7 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 01:17 PM

Sorry thats my bad.

Tbx_ImagePath.text is a textbox which allows the user to enter where the image is saved in.

The problem that I am having , it is saving and retrieving fine however because i have set the image file path to be tbx_imagepath.text once i clear this textbox and then try to retrieve the image again it will basically not load anything which makes sense as it relies on the text within the textbox. Is there a way were i can set it so that the user can save this image and then retrieve automatically not having to manually enter in the textbox
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5143
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 01:34 PM

Oh ok sure. There are many ways you can save that type of data. You can create a user/application setting, setup a configuration file where you save that value etc. I would recommend settings myself since this type of data sounds like it should probably be held with the application and the user. Below is an article you can check out about how to set those things up. Settings are great way for you to save the image path. Then it can be read back at any time and used to load the image again. You can also update it at any time during the execution of the program. The part you may find most useful is the topic "Accessing or changing application settings at run time in Visual Basic" on that page.

https://docs.microso...settings-dotnet

If you don't want to save the path to the image and instead the image itself, then you can have the program save the image to disk and then store the path to that image in settings. :)

This post has been edited by Martyr2: 28 December 2017 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#9 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 02:09 PM

You have helped so much but I just cant get it to work :(

cant get it to work so that for example if two different users register with the system;

user 1
name = John
Surname = Wallis
ImagePath = C:\Users\Sam\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\John.jpg

user 2
name = Tom
Surname = Felin
ImagePath = C:\Users\Sam\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\Tom.jpg

Cant have it so that they will be able to save these images and retrieve them when i search their names
all the other details are saved and retrieve fine
Was This Post Helpful? 0
  • +
  • -

#10 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 02:33 PM

I have the following code


its just the saving and loading image part which is incorrect ill show you what i have done

it writes the path fine with the record, however in the example provided above
i will be able to see the image of john but when i create tom and with his filepath he will always be displayed in the picturebox for some reason


Dim UserImage As Image

   'Adding[CreateUserButton]
   User.ImagePath = Tbx_ImagePath.Text
   UserImage = Image.FromFile(Tbx_ImagePath.Text)

  
  'Retreiving[FindUserButton]
  Pbx_AdminImage.Image = AdminImage


Module ModuleUser

    Structure User
        
        <VBFixedString(50)> Public ImagePath As String
       <VBFixedString(20)> Public Name As String
 <VBFixedString(20)> Public Surname As String


    End Structure



Was This Post Helpful? 0
  • +
  • -

#11 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5143
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 03:17 PM

Where are you reading the file / settings at? Could you possibly be reading in the records in a loop and then attempt to load the picture which is going to always be the last image? Because I bet if you created a third user, it would show their image in the box instead of Tom right? Sounds like you are just loading the wrong user. Which I have to say is a bit different than your original problem statement of you not being able to load images at all. At least that is what I gathered.

Are you reading the users in as a loop from the file? If so, you are going to need to read in the users, determine which needs to be shown, then show the appropriate ImagePath.
Was This Post Helpful? 0
  • +
  • -

#12 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 03:23 PM

im not sure this is my readcode:

  Dim User As Worker

        FileNum = FreeFile()
        If Tbx_UDSearch.Text = "" Then
            MsgBox("Please Enter The UserID You Would Like To Search For")
        Else
            FileOpen(FileNum, Tbx_File.Text, OpenMode.Random, , , Len(User))
            Do While Not EOF(FileNum)
                FileGet(FileNum, User, )
                If User.UserID = Val(Tbx_UDSearch.Text) Then
                    Position = Loc(FileNum)

                    Tbx_UDForename.Text = User.Forename
                    Pbx_UserImage.Image = UserImage

                    Exit Do
                End If
            Loop
            FileClose(FileNum)
        End If
    End Sub





the admin and user are all under workers btw so they all use same code

This post has been edited by WizWazard: 28 December 2017 - 03:22 PM

Was This Post Helpful? 0
  • +
  • -

#13 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5143
  • View blog
  • Posts: 13,832
  • Joined: 18-April 07

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 05:02 PM

You are working in VB 6? or VB.NET? They are pretty different things now. If you are using VB.NET you really shouldn't be using Visual Basic functions like Len, FileOpen, MsgBox etc. They may work if you bring in the VisualBasic namespace, but they are prone to errors and will break in the future.

In your code, where are you setting UserImage? That should be set at the time you are looping...

Do While Not EOF(FileNum)
    FileGet(FileNum, User, )
    If User.UserID = Val(Tbx_UDSearch.Text) Then ' Make sure this is only being executed for the right user ID
        Position = Loc(FileNum)

        Tbx_UDForename.Text = User.Forename
        Pbx_UserImage.Image = Image.FromFile(User.ImagePath) ' Or whatever you are using to get the image path from the file

        Exit Do
    End If
Loop


Was This Post Helpful? 0
  • +
  • -

#14 WizWazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-November 17

Re: Saving image as a pathfile to textfiles

Posted 28 December 2017 - 05:27 PM

im not sure what you mean

admin and user have the same code

this is my create admin button :


      

        Dim Admin As AdminType, LastRecord As Integer, Duplicate As Boolean
        LastRecord = FindLastRecordNo()
        FileNum = FreeFile()
        If Tbx_AdminID.Text = "" Then
            MsgBox("Enter a unique Admin ID")

        ElseIf Not Tbx_AdminID.Text.Length = 10 Then

            MsgBox("Please Enter a 10 didgit Unique Admin ID")

        ElseIf (Tbx_Forename.Text <> "") And (Tbx_Surname.Text <> "") And (Tbx_Password.Text <> "") And (Tbx_DOB.Text <> "") And (Tbx_ContactNumber.Text <> "") And (Tbx_EmergencyContact.Text <> ") AND (tbx_AddressTwo.text <> ") And (Tbx_PostCode.Text <> "") Then


            FileOpen(FileNum, Tbx_File.Text, OpenMode.Random, , , Len(Admin))
            Admin.AdminID = Val(Tbx_AdminID.Text) 'error possibly
            Admin.Forename = Tbx_Forename.Text
            Admin.Surname = Tbx_Surname.Text
            Admin.Password = Tbx_Password.Text
            Admin.DateOfBirth = Tbx_DOB.Text
            Admin.ContactNumber = Tbx_ContactNumber.Text
            Admin.EmergencyContact = Tbx_EmergencyContact.Text
            Admin.AddressOne = Tbx_Address1.Text
            Admin.AddressTwo = Tbx_Address2.Text
            Admin.PostCode = Tbx_PostCode.Text
            Admin.ImagePath = Tbx_ImagePath.Text
            AdminImage = Image.FromFile(Tbx_ImagePath.Text)


            
            FilePut(FileNum, Admin, LastRecord)
            FileClose(FileNum)
            ClearAddAdmin()





this is my search button :



 Dim Admin As AdminType

        FileNum = FreeFile()
        If Tbx_UDSearch.Text = "" Then
            MsgBox("Please Enter The AdminID You Would Like To Search For")
        Else
            FileOpen(FileNum, Tbx_File.Text, OpenMode.Random, , , Len(Admin))
            Do While Not EOF(FileNum)
                FileGet(FileNum, Admin, )
                If Admin.AdminID = Val(Tbx_UDSearch.Text) Then
                    Position = Loc(FileNum)

                    Tbx_UDForename.Text = Admin.Forename
                    Tbx_UDSurname.Text = Admin.Surname
                    Tbx_UDPassword.Text = Admin.Password
                    Tbx_UDDOB.Text = Admin.DateOfBirth
                    Tbx_UDContactNumber.Text = Admin.ContactNumber
                    Tbx_UDEmergencyContact.Text = Admin.EmergencyContact
                    Tbx_UDAddress1.Text = Admin.AddressOne
                    Tbx_UDAddress2.Text = Admin.AddressTwo
                    Tbx_UDPostCode.Text = Admin.PostCode
                    Pbx_AdminImage.Image = AdminImage

                    Exit Do
                End If
            Loop
            FileClose(FileNum)
        End If
    End Sub




Dim FileNum As Integer
Dim Position As Integer
Dim AdminImage As Image

are global variables
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1