|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic File Routines Pure VB: Combine a Picture and Text into a Single File |
||
Posted: | Sunday May 07, 2000 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB3, VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows NT4 | |
OS restrictions: | None | |
Author: | Larry Serflaten, VBnet - Randy Birch | |
Related: |
Pure VB: Combine Multiple Elements into a Single File RegSetValueEx: Create a Registered File Association |
|
Prerequisites |
None. |
|
This
question appears so often in the newsgroups as to almost qualify as a FAQ. In answering one such question, Larry Serflaten provided this
answer.
This demo takes the contents of a picture (or image) control, and the contents of a text box, and creates a composite file that contains both. The key to this method (which in this demo saves the picture followed by the textbox text) is saving to the end of the file an additional Long value that represents the picture size; a value that indicates where in the file one block of data ends and the next block begins. On loading the file, the Long value is retrieved first, a byte array is dimensioned to the size required to load the image data, and the picture is loaded into the byte array. The remaining portion of the file (minus the trailing Long) is retrieved directly back into the text box. Both steps use VB's Binary file mode; no APIs are required. To create this demo load any picture into a picture box, and any text into the textbox. The size of the data placed into the textbox is of no consequence, unless of course such text exceeds the text control's maximum capacity. |
BAS Module Code |
None. |
|
Form Code |
Drop a picture box (Picture1), a text box (Text1) and three command buttons (Command1-Command3) onto a form. Load a picture and populate the text box, then add the following code. Be sure to set the Const paths in the general declarations to valid files and paths on your system. |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Const sCombinedFile = "d:\vb5\test\combined.dat" Const sTmpPix = "d:\vb5\test\picxtract.bmp" Private Sub Command1_Click() Dim nImageSize As Long Dim hFile As Long 'save the picture portion as the first entry 'in the "combined file" SavePicture Picture1.Picture, sCombinedFile 'Open the combined file for append in order to 'add the text to file hFile = FreeFile Open sCombinedFile For Binary As #hFile 'Retrieve the size of the image into a 'variable for later use, then append the 'text into the same file. nImageSize = LOF(hFile) Seek #hFile, nImageSize + 1 Put #hFile, , Text1.Text 'The file now contains both the image 'and text file. As a final step, we 'save the length of image retrieved 'above as the last item in the file. Seek #hFile, LOF(hFile) Put #hFile, LOF(hFile) + 1, nImageSize Close #hFile End Sub Private Sub Command2_Click() Set Picture1.Picture = Nothing Text1.Text = "" End Sub Private Sub Command3_Click() Dim nImageSize As Long Dim hFile As Long Dim hFileOut As Long Dim PicData() As Byte 'First step in the extraction process is to 'obtain the length of image portion saved 'as the last item in the file. hFile = FreeFile Open sCombinedFile For Binary As #hFile 'move to the LOF - 3 and load the 'saved image size Seek #hFile, LOF(hFile) - 3 Get #hFile, , nImageSize 'with the image size, create a byte array 'large enough to accommodate the image ReDim PicData(0 To nImageSize - 1) As Byte 'and load the image data, repositioning the 'file pointer to the beginning first Seek #hFile, 1 Get #hFile, , PicData() 'write the pix to a temporary file in 'order to use the LoadPicture method. hFileOut = FreeFile Open sTmpPix For Binary As #hFileOut Put #hFileOut, , PicData() Close #hFileOut 'load the text portion to the textbox. The 'text begins immediately after the image, and 'extends for the file length minus the trailing '4 bytes for the Long that stored the image size. Seek #hFile, nImageSize + 1 Text1.Text = Input(LOF(hFile) - nImageSize - 4, hFile) Close #hFile 'Load the saved image from the tmp file 'and kill it Picture1 = LoadPicture(sTmpPix) Kill sTmpPix End Sub |
Comments |
Additional code should be added before reading the files to assure that the files exist. You can find FileExists methods on the FileAPI page. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |