This code shows how to use MemoryStream class and its member to read data in memory stream, which can be used to save it from there.
using System;
using System.IO;
using System.Text;
class MemoryStreamSample
{
static void Main()
{
int count;
//GetByteData function to get Byte data like if you fetch Image column data from sqlserver or somewhere.
byte[] byteArray = getByteData();
MemoryStream memStream = new MemoryStream(ByteArray);
// Write the second string to the stream, byte by byte.
count = 0;
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = {0}, Length = {1}, Position = {2}\n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
// Read the remaining bytes, byte by byte.
while(count < memStream.Length)
{
byteArray[count++] = Convert.ToByte(memStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
}
}
Amine EnnouriPosted Oct 24, 2017, 5:26 AM
How can i read the memory stream and display pdf file ?
SubashPosted Sep 7, 2016, 4:05 AM
Nice
santosh kumarPosted Jan 18, 2013, 4:29 AM
Can you please help to modified that code.
santosh kumarPosted Jan 18, 2013, 4:28 AM
public static byte[] Decompress(byte[] data) { using (MemoryStream input = new MemoryStream()) { input.Write(data, 0, data.Length); input.Position = 0; using (GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true)) { using (MemoryStream output = new MemoryStream()) { byte[] buff = new byte[64]; int read = -1; read = gzip.Read(buff,0, buff.Length); while (read > 0) { output.Write(buff, 0, read); read = gzip.Read(buff, 0, buff.Length); } gzip.Close(); return output.ToArray(); } } } } Getting Error Like this:- The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
DavidPosted Jul 11, 2010, 6:17 PM
Way to copy from http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx