We just created the byte array. here is the code below
FileStream stream = File.OpenRead(imageFilePath); var b = new byte[stream.Length]; stream.Read(b, 0, b.Length);
With Framework .NET 4 and above, I had use Stream.CopyTo. Create the MemoryStream, call stream.CopyTo(ms) and then return ms.ToArray(). The convert is done.
public static byte[] ToByteArray(Stream input) { byte[] buffer = new byte[16*1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } }
With the Stream.Read statement you will not always be able to read everything that is asked of you. If you’re reading from a network stream, for example, you can read the value of a packet and then return, even if there will be more data soon. BinaryReader.Read will continue to the end of the stream or to the specified size, but must still know the size to start.
You can even make the code a bit more elegant with extensions:
namespace Foo { public static class Extensions { public static byte[] ToByteArray(this Stream stream) { using (stream) { using (MemoryStream memStream = new MemoryStream()) { stream.CopyTo(memStream); return memStream.ToArray(); } } } } }
And then call it as a regular method:
byte[] array = imageStream.ToByteArray()
Read(Byte[], Int32, Int32)
Reads a block of bytes from the stream and writes the data in a given buffer.
Parameters
array Byte[]
When this method returns, contains the specified byte array with the values between offset and (offset + count – 1) replaced by the bytes read from the current source.
offset Int32
The byte offset in array at which the read bytes will be placed.
count Int32
The maximum number of bytes to read.
Returns Int32
The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.
Example in WPF with high performance
/// Get File from HDD ///Path /// public BitmapImage GetFile(string imageFilePath) { FileStream stream = File.OpenRead(imageFilePath); var b = new byte[stream.Length]; stream.Read(b, 0, b.Length); MemoryStream memoryStream = new MemoryStream(b); stream.Close(); stream.Dispose(); BitmapImage source = new BitmapImage(); source.BeginInit(); source.StreamSource = memoryStream; //listI1[i]; source.EndInit(); source.Freeze(); return source; }
Reading and recording files
FileStream provides access to files at the byte level, so, for example, if you have to count or write one or more lines into a text file, the array of bytes should be converted into strings using special methods. That’s why other classes are used to work with text files.
At the same time, when dealing with different binary files that have a specific structure, FileStream can be very useful for extracting certain portions of information and processing it.
Let’s look at the example of reading-record in a text file:
namespace HelloApp { class Program { static void Main(string[] args) { // create a directory for the file string path = @"C:\SomeDir3"; DirectoryInfo dirInfo = new DirectoryInfo(path); if (!dirInfo.Exists) { dirInfo.Create(); } Console.WriteLine("Enter the line to write in the file:"); string text = Console.ReadLine(); // entry into the file using (FileStream fstream = new FileStream($"{path}\note.txt", FileMode.OpenOrCreate)) { // transform the string into bytes byte[] array = System.Text.Encoding.Default.GetBytes(text); fstream.Write(array, 0, array.Length); Console.WriteLine("The text is written into the file"); } // Reading from the file using (FileStream fstream = File.OpenRead($"{path}\note.txt")) { // transform the string into bytes byte[] array = new byte[fstream.Length]; // reading the data fstream.Read(array, 0, array.Length); // decod bytes in a row string textFromFile = System.Text.Encoding.Default.GetString(array); Console.WriteLine($"Text from the file: {textFromFile}"); } Console.ReadLine(); } } }