Results 1 to 4 of 4

Thread: Saving Image to text file and retrieving

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    16

    Saving Image to text file and retrieving

    Hi ! I would like to save and retrieve image from textfile. I have used the following technique:

    so the user fills their id and name and surname alongsides the filepath of the image then it saves it. When retrieving it I set the picture box = to the file image but it says you cant convert string into system.draw

    any suggestions ? I am saving the imagepath to a textfile using random access

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Saving Image to text file and retrieving

    If you are trying to set the Picture like this:

    Code:
    Picture1.Picture = strucname.imagepath
    change it to this:

    Code:
    Picture1.Picture = LoadPicture(strucname.imagepath)

    Edit: The error you referenced makes me think this might be VB.NET and not VB 6.0 or earlier. If so:

    Try:

    Code:
    Picture1.Image = Image.FromFile(strucname.imagepath)
    Last edited by OptionBase1; Dec 22nd, 2017 at 02:27 PM.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Saving Image to text file and retrieving

    The use of System.Draw suggests that this is .NET. Therefore, I have moved the thread. If this is incorrect, send a PM to a moderator and we'll move it back.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Saving Image to text file and retrieving

    Quote Originally Posted by Itsmethecoder123 View Post
    so the user fills their id and name and surname alongsides the filepath of the image then it saves it. When retrieving it I set the picture box = to the file image but it says you cant convert string into system.draw
    That's not what it says. When posting error messages, please post the actual error message. If you post a vague approximation then there's every chance that we won't actually know what it means.

    In this case though, the issue seems fairly clear. Most likely you are assigning the path of the image file to the Image property of the PictureBox, e.g.
    vb.net Code:
    1. myPictureBox.Image = imageFilePath
    That's clearly nonsensical because the Image property is type Image, so you can only assign an Image object to it. A String is not an Image object, even if it contains the path of a file that contains data that can be used to construct an Image object. You have two main options:

    1. In times gone by, you would have had to create an Image object yourself and then assign that to the Image property, e.g.
    vb.net Code:
    1. myPictureBox.Image = Image.FromFile(imageFilePath)
    An important point to note is that that will lock the file until you dispose the Image object, so you absolutely should dispose the Image object when you're done with it. A variation on this option is to use the Bitmap constructor if the image is a bitmap type, e.g.
    vb.net Code:
    1. myPictureBox.Image = New Bitmap(imageFilePath)
    The same rules regarding disposal apply.

    2. In more recent versions of .NET, you have been able to set the ImageLocation property of the PictureBox and let it create the Image object itself, e.g.
    vb.net Code:
    1. myPictureBox.ImageLocation = imageFilePath
    That will support local file paths or URLs and will display an error image if the path is invalid. This option doesn't lock the file, so you can move or delete the file while it's being displayed. A variation on this option is to call the Load method, e.g.
    vb.net Code:
    1. myPictureBox.Load(imageFilePath)
    That said, I have a feeling that I tested some time ago and found, to my surprise, that that did lock the file, which I had previously thought was not the case. You ought to test that for yourself to see.

    Note that one issue with storing the image file path is that the file may be moved or deleted. If that is a possible issue then you may need to store the image itself. That's generally not a good idea because of the potential size of the data but sometimes it's required. In that case, you can load the image contents into a Byte array and then convert that to a base-64 string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width