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?
Printable View
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?
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.You can then use the following code to read the file.VB Code:
Public Sub SaveFile(ByVal sFileName As String) Dim bArr() As Byte Dim hFile As Integer Dim pb As PropertyBag Set pb = New PropertyBag 'Save the things in the property bag pb.WriteProperty "Pic", Picture1.Picture, Nothing pb.WriteProperty "Txt", Text1.Text, "" 'get the content bArr = pb.Contents 'open the file and save hFile = FreeFile Open sFileName For Binary Access Write As #hFile Put #hFile, , bArr Close #hFile End SubOf course you should add error trapping in the code above.VB Code:
Public Sub ReadFile(ByVal sFileName As String) Dim hFile As Integer Dim bArr() As Byte Dim pb As PropertyBag hFile = FreeFile Open sFileName For Binary Access Read As #hFile ReDim bArr(LOF(hFile) - 1) Get #hFile, , bArr Close #hFile Set pb = New PropertyBag pb.Contents = bArr Picture1.Picture = pb.ReadProperty("Pic", Nothing) Text1.Text = pb.ReadProperty("Txt", "") End Sub
Looks great, I'll try it out as soon as I can.
Thanks, works perfectly.