Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jpeg image to byte array

Posted on 2006-07-04
7
565 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

829 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