Results 1 to 4 of 4

Thread: Creating a file... [Resolved]

  1. #1

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Creating a file... [Resolved]

    I'd like to take a bitmap and a text file, and turn them into one file. How can I compile them as such and then be able to read them afterwards?
    Last edited by Nove; Mar 19th, 2005 at 12:24 AM.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Creating a file...

    The easiest way I've found to combine all kind of different information in a file is to use a PropertyBag. Of course only your application (or any that knows exactly how you've made it) will be able to read it.

    This example takes a picture loaded into a PictureBox, named Picture1, and a text shown in a TextBox, named Text1, and save it to a supplied file name.
    VB Code:
    1. Public Sub SaveFile(ByVal sFileName As String)
    2.     Dim bArr() As Byte
    3.     Dim hFile As Integer
    4.     Dim pb As PropertyBag
    5.  
    6.     Set pb = New PropertyBag
    7.     'Save the things in the property bag
    8.     pb.WriteProperty "Pic", Picture1.Picture, Nothing
    9.     pb.WriteProperty "Txt", Text1.Text, ""
    10.     'get the content
    11.     bArr = pb.Contents
    12.     'open the file and save
    13.     hFile = FreeFile
    14.     Open sFileName For Binary Access Write As #hFile
    15.     Put #hFile, , bArr
    16.     Close #hFile
    17. End Sub
    You can then use the following code to read the file.
    VB Code:
    1. Public Sub ReadFile(ByVal sFileName As String)
    2.     Dim hFile As Integer
    3.     Dim bArr() As Byte
    4.     Dim pb As PropertyBag
    5.  
    6.     hFile = FreeFile
    7.     Open sFileName For Binary Access Read As #hFile
    8.     ReDim bArr(LOF(hFile) - 1)
    9.     Get #hFile, , bArr
    10.     Close #hFile
    11.     Set pb = New PropertyBag
    12.     pb.Contents = bArr
    13.     Picture1.Picture = pb.ReadProperty("Pic", Nothing)
    14.     Text1.Text = pb.ReadProperty("Txt", "")
    15. End Sub
    Of course you should add error trapping in the code above.

  3. #3

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Creating a file...

    Looks great, I'll try it out as soon as I can.

  4. #4

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Creating a file... [Probably Resolved]

    Thanks, works perfectly.

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