Solved

jpeg image to byte array

Posted on 2006-07-04
7
563 Views
Last Modified: 2008-03-10
Can anybody give me an example of how I can store a jpeg image (either from the filesystem or from a SQL BLOB) into a byte array in VB6?
0
Comment
Question by:Tricky3
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17039739
Here is code that will open a jpeg, read it into a byte array, then write out the byte array to another file:

Option Base 1
Private Sub Form_Load()
Dim strFilePath As String
Dim lngFileSize As Long
Dim dblFree As Double
Dim byteArray() As Byte

strFilePath = "c:\file.jpg"

dblFree = FreeFile
Open strFilePath For Binary As #dblFree
    lngFileSize = LOF(dblFree)
    ReDim byteArray(lngFileSize)
    Get #dblFree, 1, byteArray()
Close #dblFree
   
Open "c:\output.jpg" For Binary As #2
    Put #2, 1, byteArray()
Close #2

   
   
End Sub
0
 

Author Comment

by:Tricky3
ID: 17039782
Thanks for that - I'll accept the answer, but was hoping you may be able to help me take this one step further.

Is there any way I could add additional data to this array, eg text and even some more images?

The get function appears to just overwrite the entire array..

Thanks again

0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 250 total points
ID: 17039801
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17045782
>> The get function appears to just overwrite the entire array..

Acctually the ReDim erases the whole array. If you add the keyword 'Preserve' after the word ReDim, it will Redimension the array and preserve the contents. Make sense?
0
 

Author Comment

by:Tricky3
ID: 17046798
It does make sense thanks, I realised this afterwards.

As I need a mixture of JPEG & Text data (for eventually posting as a multipart form via xmlhttp) I am using 2 byte arrays. One I use to collect each individual text/jpeg portion and i then copy it into the second array which is the final version that gets posted. It's working fine this way.

Thanks for your help.

0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17047352
I see, you will likely run across problems if you turn the data into a string, its common to find null terminators in binary files which will terminate a string.

brian
0
 

Author Comment

by:Tricky3
ID: 17047370
Thanks Brian,

You're spot on - that's exactly what was happening originally (had me baffled for a while) which is why I went for the byte array option. Thankfully you can pass the whole byte array to the send method of the xmlhttp object for the post.

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question