Latest Post

Sunday, 18 September 2016

How to Convert Stream To Byte Array In C#

Hello everyone, I am sharing  code to convert Stream To Byte Array using the ASP.Net C#.

public static byte[] StreamToByteArray(Stream stream)
        {
            try
            {
                byte[] result;
                byte[] buffer = new byte[4096];
                using (Stream responseStream = stream)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int count = 0;
                        do
                        {
                            count = responseStream.Read(buffer, 0, buffer.Length);
                            ms.Write(buffer, 0, count);
                        } while (count != 0);
                        result = ms.ToArray();
                    }
                }
                return result;
            }
            catch
            {
                throw;
            }

         }

Government Jobs