If I were doing this, I might store the length of the image data just before it, and once you read that line, you will know how much data to read. Another way might be to use a UDT, which could load various sorts of data all at once.