• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1173
  • Last Modified:

Save WinHTTP.ResponseBody as File : Open It Again

1) I am using WinHTTP 5 to fetch an image from the network

2) Then I do this:
       
          Dim MyBytes() As Byte
          MyBytes = objWinHTTP.ResponseBody

3) Now, I want to save the MyBytes array to an image file

4) Then I want to open the image again (later) and put it into the MyBytes array

===========================
So, I need basically two functions:

              Function One: Takes a Byte Array and saves it to a file

              Function Two: Opens a file and puts it into a Byte Array


0
ScribbleMeat
Asked:
ScribbleMeat
  • 3
  • 3
1 Solution
 
mvidasCommented:
Hi ScribbleMeat,

The following should do it for you:

 Dim vFF As Long
 vFF = FreeFile
 
 'save byte array to file
 Open "C:\imagename.jpg" For Binary As #vFF
 Put #vFF, , MyBytes
 Close #vFF
 
 'create byte array from file
 Open "C:\imagefile.jpg" For Binary As #vFF
 ReDim MyBytes(LOF(vFF) - 1)
 Get #vFF, , MyBytes
 Close #vFF

When using the Get statement to fill a byte array, you need to dimension the array to be the number of bytes in the file.  You can get this using LOF on the open file (or FileLen on a closed file), but because the array starts at index 0, you need to decrease this by 1.

Please let me know if you have any questions!
Matt
0
 
ScribbleMeatAuthor Commented:
mvidas,

So, I turned your code into two functions. Do you see any problems or mistakes I made, or is it good to go?

==========================================
Public Sub SaveBinary(FilePath As String, ByteArray As Byte)

            Dim vFF As Long
            vFF = FreeFile
           
            'save byte array to file
            Open FilePath For Binary As #vFF
            Put #vFF, , ByteArray
            Close #vFF

 End Sub
 
 
Public Sub OpenBinary(FilePath As String, ByRef ByteArray As Byte)
           
            Dim vFF As Long
            vFF = FreeFile
           
            'create byte array from file
            Open FilePath For Binary As #vFF
            ReDim MyBytes(LOF(vFF) - 1)
            Get #vFF, , ByteArray
            Close #vFF

End Sub
===================================

-- ScribbleMeat
0
 
ScribbleMeatAuthor Commented:
I meant subs, not functions -- duh
0
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!

 
mvidasCommented:
The OpenBinary needed a couple changes, and you can see them in the sub below.  I think for the OpenBinary it would be better to have it as a function, so I made a fOpenBinary function.  I also have two test subs (which all copy an excel file called test.xls), one using the sub method and one with the function for openbinary.  No real speed difference, just an easier way of doing it (in my opinion at least).  I made a third test sub as well, which essentially copies a file using these two routines in a single line.  Thats really the only advantage to have fOpenBinary as a function.

Public Sub SaveBinary(ByVal FilePath As String, ByRef ByteArray() As Byte)
    Dim vFF As Long
    vFF = FreeFile
   
    'save byte array to file
    Open FilePath For Binary As #vFF
    Put #vFF, , ByteArray
    Close #vFF
 End Sub
Public Sub OpenBinary(ByVal FilePath As String, ByRef ByteArray() As Byte)
    Dim vFF As Long, MyBytes() As Byte
    vFF = FreeFile
   
    'create byte array from file
    Open FilePath For Binary As #vFF
    ReDim MyBytes(LOF(vFF) - 1)
    Get #vFF, , MyBytes
    Close #vFF
    ByteArray = MyBytes
End Sub

Public Function fOpenBinary(ByVal FilePath As String) As Byte()
    Dim vFF As Long, MyBytes() As Byte
    vFF = FreeFile
   
    'create byte array from file
    Open FilePath For Binary As #vFF
    ReDim MyBytes(LOF(vFF) - 1)
    Get #vFF, , MyBytes
    Close #vFF
    fOpenBinary = MyBytes
End Function

Sub TestWithSubs()
 Dim bArray() As Byte
 OpenBinary "C:\test.xls", bArray
 SaveBinary "C:\test2.xls", bArray
End Sub

Sub TestWithTheFunction()
 Dim bArray() As Byte
 bArray = fOpenBinary("C:\test.xls")
 SaveBinary "C:\test3.xls", bArray
End Sub

Sub TestWithTheFunction2()
 SaveBinary "C:\test4.xls", fOpenBinary("C:\test.xls")
End Sub



I realize this could be a little confusion, so let me know if you have any questions!
Matt
0
 
ScribbleMeatAuthor Commented:
Oh, I see. I actually made OpenBinary a function first, but for some reason I didn't think to have the function return "As Byte()" and instead I returned it "As Byte." It makes a lot more sense your way. I often get confused when working with arrays because in my programming I tend to work more with collections and hives which can be handled as objects.

Well, obviously you get the points here. But I just wanted to say that I wish that there were more people like you here on EE. Not just because you know what you are talking about, but because you are a great communicator. I have had some other great experiences here on EE with Ark, zzzzoc, and AzraSound and I wish that there was some way to create a circle of experts that you could count on to be knowledgeable, curteous and above all thorough. But it is nearly impossible to ask someone a question directly on here.

I always stay subscribed to questions that has good experts commenting. So if you ever need my help on anything, just drop a note in here and point me to your question. My best areas are Windows API, shell integration, window manipulation, and external class control. I also hobby on GUI design (maybe someday I will put it all together and make a skinning component.)

Good to meet ya,

-- Tim (aka ScribbleMeat)
0
 
mvidasCommented:
Hey Tim,

I definately understand about arrays being confusing, I didn't really get heavily into arrays until the past 2 or 3 months as I couldn't get my head past them.  But as with anything, time + practice = knowledge, and now I find myself wanting to use arrays for most everything!  I'm pretty new to programming, myself. I actually use Excel VBA for almost all my programming (including this), and have never used WinHTTP before.  But I've used xmlHTTP (and not even that much) so I assumed it was either the same or similar.

I always stay subscribed to all questions, so if you need help with other questions you can always just post another comment here with a link to the other question or send me an email with the same, my email is in my profile.

I just saw you were relatively new, welcome to EE! Quite a fun and worthwhile addiction.  A year and 2 months ago I couldn't write a single thing (except some basic uses of the excel object that I found while recording macros), but thanks to this site I've learned a lot!  And still learn new things every day here.

I'm sure I'll be seeing you around, but let me know if you need anything!
Matt
0

Featured Post

Technology Partners: 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!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now