How to read exactly byte, then seek until there is null byte, and write the seeked byte to a binary file.
Public Function FindBytes(ByVal src As Byte(), ByVal find As Byte()) As Integer
Dim index As Integer = -1
Dim matchIndex As Integer = 0
For i As Integer = 0 To src.Length - 1
If src(i) = find(matchIndex) Then
If matchIndex = (find.Length - 1) Then
index = i - matchIndex
Exit For
End If
matchIndex += 1
Else
matchIndex = 0
End If
Next
Return index
End Function
Private Function GetBytes(ByVal filename As String) As Byte()
Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
Dim ImageData As Byte() = New Byte(fs.Length - 1) {}
fs.Read(ImageData, 0, System.Convert.ToInt64(fs.Length))
fs.Close()
Return ImageData
End Function
Dim PNGStart As Byte() = {&H89, &H50, &H4E, &H47}
Dim PNGEnd As Byte() = { &H49, &H45, &H4E, &H44, &HAE, &H42, &H60, &H82}
Dim StartPosition As Integer = FindBytes(GetBytes(filename), PNGStart)
SO How to Loop throw PNGStart to PNGEnd and write it to a file?
Note: I have found
FindBytes
function in other site, but I don't remember it's name, credits to the one who created it.