?
Solved

jpeg image to byte array

Posted on 2006-07-04
7
Medium Priority
?
591 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 1000 total points
ID: 17039801
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

755 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