I.Convert binary to image
Memorystream MS = new memorystream (bytes );
Ms. Position = 0;
Image IMG = image. fromstream (MS );
Ms. Close ();
This. picturebox1.image
II.Conversion between byte [] and string in C #Code
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 = new filestream (filename, filemode. Open, fileaccess. Read, fileshare. Read );
III.Conversion between C # stream and byte []
/// Convert stream to byte []
Public byte [] streamtobytes (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;
}
4.Stream and file conversion
Write stream to file
Public void streamtofile (Stream stream, string filename)
{< br> // converts stream to byte []
byte [] bytes = new byte [stream. length];
stream. read (bytes, 0, bytes. length );
// set the starting position of the current 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 File
Public stream filetostream (string filename)
{
// Open the file
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;
}
Reprinted from: http://www.myext.cn/csharp/6415.html