Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with image file 1

Status
Not open for further replies.

Nordyck

Programmer
Aug 16, 2001
57
US
What I'm trying to do is read a image file in (which works), and then rewrite it.

The problem is in the rewriting it.

I can't just copy it because I'll be doing some bit manipulation before it's written back out.
Tried various stuff and it didn't work.



Sub test
Dim binfile As Variant
binfile = readBinFile(Text1)
WriteBinFile "c:\msat\coin3.ico", binfile
exit sub

Function readBinFile(ByRef bfilename As String) As Variant
Dim fl As Long
Dim FileNum As Long
Dim binbyte() As Byte
Dim binfilestr As String

On Error GoTo errHandler

FileNum = FreeFile
'bfilename = Text1
Open bfilename For Binary Access Read As #FileNum

fl = FileLen(bfilename)
ReDim binbyte(fl)

Get #FileNum, , binbyte

Close #FileNum

readBinFile = binbyte
Exit Function

errHandler:
End Function

Function WriteBinFile(ByRef bfilename As String, ByRef binbyte As Variant) As Variant
Dim fl As Long
Dim FileNum As Long
'Dim binbyte() As Byte
Dim binfilestr As String
Dim i As Long

On Error GoTo errHandler

FileNum = FreeFile
Open bfilename For Binary As #FileNum

For i = 0 To UBound(binbyte) - 1
binfilestr = binfilestr & binbyte(i)
Next i
Put #FileNum, , binfilestr

Close #FileNum

'readBinFile = binbyte
Exit Function

errHandler:
End Function


nordycki@hotmail.com
 
The problem was caused by converting to Variant. Remember, VB stores Unicode, not Bytes...

Code:
Option Explicit

Private Sub Form_Load()
test
End Sub



Sub test()
    Dim binfile() As Byte
    binfile = readBinFile("c:\arrow1.ico")
    WriteBinFile "c:\arrow2.ico", binfile
End Sub

Function readBinFile(ByRef bfilename As String) As Variant
    Dim fl As Long
    Dim FileNum As Long
    Dim binbyte() As Byte
    Dim binfilestr As String

    On Error GoTo errHandler

    FileNum = FreeFile
    'bfilename = Text1
    Open bfilename For Binary Access Read As #FileNum

    fl = FileLen(bfilename)
    ReDim binbyte(fl)

    Get #FileNum, , binbyte

    Close #FileNum

    readBinFile = binbyte
    Exit Function

errHandler:
End Function

Function WriteBinFile(ByRef bfilename As String, ByRef binbyte() As Byte) As Variant
    Dim fl As Long
    Dim FileNum As Long
    'Dim binbyte() As Byte
    Dim binfilestr As String
    Dim i As Long

    On Error GoTo errHandler

    FileNum = FreeFile
    Open bfilename For Binary As #FileNum
        
    For i = 0 To UBound(binbyte) - 1
        binfilestr = binfilestr & binbyte(i)
    Next i
    Put #FileNum, , binbyte

    Close #FileNum

    Exit Function

errHandler:
End Function
 
Simply change
Code:
[blue]binfilestr = binfilestr & binbyte(i)[/blue]
to
Code:
[blue]binfilestr = binfilestr & Chr(binbyte(i))[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top