I have a text byte array and an image byte array. I need to add them together into a stream to be read later, and I just cant figure out how to combine them. Right now, I add the header text to a buffer, and then the image to another buffer. I then write those to a memory stream. After this, I write the total memory stream to a buffer. When I look at the data in the buffer it is correct. However, it goes bad when I try to read/write the total buffer to my network stream (NWstream). It just turns to all zeros. Basically, I need to create 2 buffers to one and then shoot it to a stream. It goes bad when I read from the final buffer to throw it in the stream. Any advice?
VB.NET:
'add header to stream
Dim ms As New MemoryStream
Dim HeaderToSend(12) As Byte
HeaderToSend = System.Text.Encoding.ASCII.GetBytes("1N" & STX & Xstring)
ms.Write(HeaderToSend, 0, 12)
'add image in to ms
Dim fs As New FileStream(ImagePath, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim ImageBytes(fs.Length) As Byte
numBytesRead = br.Read(ImageBytes, 0, ImageBytes.Length)
ms.Write(ImageBytes, 0, ImageBytes.Length)
Dim TotalReader As New StreamReader(ms)
Dim TotalBytes(ms.Length) As Byte
'****Does bad after this
Dim TR = ms.Read(TotalBytes, 0, TotalBytes.Length)
NWStream.Write(TotalBytes, 0, ImageBytes.Length)
fs.Close()
br.Close()
ms.Close()