A. Convert binary into picture
MemoryStream ms = new MemoryStream (bytes);
Ms. Position = 0;
Image img = image.fromstream (ms);
Ms. Close ();
This.pictureBox1.Image
Two. C # byte[] and string conversion 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 FileStream = new FileStream (FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Three. Conversion between C # Stream and byte[]
Turn the Stream into byte[]
Public byte[] Streamtobytes (Stream stream)
{
byte[] bytes = new Byte[stream. Length];
Stream. Read (bytes, 0, bytes. Length);
Sets the current stream's position as the start of the stream
Stream. Seek (0, seekorigin.begin);
return bytes;
}
Turn byte[] into a Stream
Public Stream bytestostream (byte[] bytes)
{
Stream stream = new MemoryStream (bytes);
return stream;
}
Four. Conversion between Stream and file
Write Stream to File
public void Streamtofile (Stream stream,string fileName)
{
Convert the Stream into byte[]
byte[] bytes = new Byte[stream. Length];
Stream. Read (bytes, 0, bytes. Length);
Sets the current stream's position as the start 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 ();
}
Five. Read Stream from File
Public Stream Filetostream (string fileName)
{
//Open file
FileStream FileStream = new FileStream (filename, F Ilemode.open, FileAccess.Read, FileShare.Read);
//Read file byte[]
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;
}