I think that changing the ToByteArray method to use StreamReader , which corresponds to the encoding, should work in this case, although without seeing more code, I can not be sure.
private byte[] ToByteArray(Stream stream, System.Text.Encoding encoding) { using(var sr = new StreamReader(stream, encoding)) { return encoding.GetBytes(sr.ReadToEnd()); } }
EDIT
Since you are working with image data, you should use Convert.ToBase64String to convert byte[] to string . Then you can use Convert.FromBase64String decoding to convert back to byte[] . The reason encoding.GetBytes does not work, because there may be some data in byte[] that cannot be represented as a string for this encoding.
private void Parse(Stream stream, Encoding encoding) { byte[] allData = ToByteArray(stream); string allContent = Convert.ToBase64String(allData); allData = Convert.FromBase64String(allContent); }
source share