You're initializing a MemoryStream:
Stream returnStream = new MemoryStream();
(should be var returnStream = new MemoryStream();
)
as the container for the bytes read from a Stream.
The Image bytes are read from the source Stream using a buffer:
var buffer = new byte[8192];
which is ok for a NetworkStream.
Assuming Uri image = new Uri(imageUrl);
represents the same object in both environments, you the read a [buffer]
number of bytes (which is the maximum value, the actual bytes read may be less than that) and write the bytes read - the value is stored in the readCount
variable - to the MemoryStream:
try
{
fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, image.AbsolutePath);
while ((readCount = fileInformation.Stream.Read(buffer, 0, buffer.Length)) != 0)
{
returnStream.Write(buffer, 0, readCount);
}
}
When the source Stream is read to end, the MemoryStream contains the Image bytes.
At this point, you want to convert the Image bytes to a Base64String.
Of course, you need to convert the content of your MemoryStream, returnStream
, not the buffer
content, which is only used as a temporary container for the bytes coming from the source Stream. So just change:
Convert.ToBase64String(buffer);
to:
Convert.ToBase64String(returnStream.ToArray());
Setting returnStream.Position = 0
before calling returnStream.ToArray()
is not necessary in this context, but it doesn't hurt either.
Side note: those empty catch
blocks don't serve you well. Either add logging features or remove.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…