C # conversion between Stream and byte,
1. Convert binary data into images
MemoryStream MS = new MemoryStream (bytes );
Ms. Position = 0;
Image img = Image. FromStream (MS );
Ms. Close ();
This. pictureBox1.Image
Ii. Code for converting byte [] and string in C #
1. System. Text. UnicodeEncoding converter = new System. Text. UnicodeEncoding ();
Byte [] inputBytes = converter. GetBytes (inputString );
String inputString = converter. GetString (inputBytes );
2. string inputString = System. Convert. ToBase64String (inputBytes );
Byte [] inputBytes = System. Convert. FromBase64String (inputString );
FileStream fileStream = new FileStream (fileName, FileMode. Open, FileAccess. Read, FileShare. Read );
Iii. Conversion between C # Stream and byte []
/// Convert Stream to byte []
Public byte [] StreamToBytes (Stream stream)
{
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, SeekOrigin. Begin );
Return bytes;
}
/// Convert byte [] to Stream
Public Stream BytesToStream (byte [] bytes)
{
Stream stream = new MemoryStream (bytes );
Return stream;
}
Iv. Conversion between streams and files
Write Stream to file
Public void StreamToFile (Stream stream, string fileName)
{
// Convert Stream to byte []
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, SeekOrigin. Begin );
// Write byte [] to a file
FileStream fs = new FileStream (fileName, FileMode. Create );
BinaryWriter bw = new BinaryWriter (fs );
Bw. Write (bytes );
Bw. Close ();
Fs. Close ();
}
5. Read Stream from a file
Public Stream FileToStream (string fileName)
{
// Open the file
FileStream fileStream = new FileStream (fileName, FileMode. Open, FileAccess. Read, FileShare. Read );
// Read the byte of the object []
Byte [] bytes = new byte [fileStream. Length];
FileStream. Read (bytes, 0, bytes. Length );
FileStream. Close ();
// Convert byte [] to Stream
Stream stream = new MemoryStream (bytes );
Return stream;
}