title: Converting MemoryStream to Byte Array in VB.Net
body: <p>I am trying to convert the memory stream generated from richeditDocument to byte array. The code is given below:</p>
  
  <pre><code>Public Sub saveAsTemplate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
      Dim ms As MemoryStream = New MemoryStream()
      richEditControl1.SaveDocument(ms, DocumentFormat.Rtf)
      GetStreamAsByteArray(ms)
  
      MessageBox.Show("save")
  
  End Sub
  
  Private Function GetStreamAsByteArray(ByVal stream As MemoryStream) As Byte()
  
      Dim streamLength As Integer = Convert.ToInt32(stream.Length)
  
      Dim fileData As Byte() = New Byte(streamLength) {}
  
      ' Read the file into a byte array
      stream.Read(fileData, 0, streamLength)
      stream.Flush()
      stream.Close()
  
      Return fileData
  
  End Function
  </code></pre>
  
  <p>The stream is generated as I can get the stream length, however the final bite array only consist of 0's making it invalid. How can I get correct byte array of it?</p>