Append byte[] to MemoryStream
13,871
startPosition
is not offset to MemoryStream, instead to ba
. Change it as
allFrameStream.Write(ba, 0, ba.Length);
All byte arrays will be appended to allFrameStream
BTW: Don't use ba = allFrameStream.GetBuffer();
instead use ba = allFrameStream.ToArray();
(You actually don't want internal buffer of MemoryStream).
Author by
Billa
Updated on September 08, 2022Comments
-
Billa over 1 year
I am trying to read the
byte[]
for each file and adding it to MemoryStream. Below is the code which throws error. What I am missing in appending?byte[] ba = null; List<string> fileNames = new List<string>(); int startPosition = 0; using (MemoryStream allFrameStream = new MemoryStream()) { foreach (string jpegFileName in fileNames) { ba = GetFileAsPDF(jpegFileName); allFrameStream.Write(ba, startPosition, ba.Length); //Error here startPosition = ba.Length - 1; } allFrameStream.Position = 0; ba = allFrameStream.GetBuffer(); Response.ClearContent(); Response.AppendHeader("content-length", ba.Length.ToString()); Response.ContentType = "application/pdf"; Response.BinaryWrite(ba); Response.End(); Response.Close(); }
Error:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection