Solved

jpeg image to byte array

Posted on 2006-07-04
7
561 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now