Solved

jpeg image to byte array

Posted on 2006-07-04
7
572 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
[X]
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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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…

691 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