C # conversion between Stream and byte,
I. BinaryConversionImage Creation
MemoryStream ms = new MemoryStream(bytes);ms.Position = 0;Image img = Image.FromStream(ms);ms.Close();this.pictureBox1.Image
2. in C #, byte [] and stringConversionCode
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. C # Between Stream and byte []Conversion
/// 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 starting stream of the stream. seek (0, SeekOrigin. begin); return bytes;} // convert byte [] to Streampublic Stream BytesToStream (byte [] bytes) {Stream stream = new MemoryStream (bytes); return stream ;}
4. Stream and fileConversion
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 starting stream of the stream. seek (0, SeekOrigin. begin); // write byte [] to the 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 [] byte [] bytes = new byte [fileStream. length]; fileStream. read (bytes, 0, bytes. length); fileStream. close (); // convert byte [] to Stream stream = new MemoryStream (bytes); return stream ;}
Sat.
// Bitmap converted to Byte [] Bitmap BitReturn = new Bitmap (); byte [] bReturn = null; MemoryStream MS = new MemoryStream (); BitReturn. save (MS, System. drawing. imaging. imageFormat. png); bReturn = ms. getBuffer ();