one. Binary conversion to picture MemoryStream Ms=NewMemoryStream (bytes); Ms. Position=0; Image img=Image.fromstream (MS); Ms. Close (); This. pictureBox1.Image two. Conversion code for byte[] and string in C #1, System.Text.UnicodeEncoding converter =NewSystem.Text.UnicodeEncoding (); byte[] Inputbytes =Converter. GetBytes (inputstring); stringInputString =Converter. GetString (inputbytes);2、stringInputString =System.Convert.ToBase64String (inputbytes); byte[] Inputbytes =System.Convert.FromBase64String (inputstring); FileStream FileStream=NewFileStream (FileName, FileMode.Open, FileAccess.Read, fileshare.read); Iii. C # Stream andbyteconversion between []///turn the Stream into byte[] Public byte[] Streamtobytes (Stream stream) {byte[] bytes =New byte[Stream. Length]; Stream. Read (Bytes,0, Bytes. Length); //sets the position of the current stream as the start of the streamStream. Seek (0, Seekorigin.begin); returnbytes;}///turn byte[] into a Stream PublicStream Bytestostream (byte[] bytes) {Stream Stream=NewMemoryStream (bytes);returnStream;} Four. Conversion between stream and file writes stream to file Public voidStreamtofile (Stream stream,stringfileName) { //convert Stream to byte[]byte[] bytes =New byte[Stream. Length]; Stream. Read (Bytes,0, Bytes. Length); //sets the position of the current stream as the start of the streamStream. Seek (0, Seekorigin.begin); //write byte[] to fileFileStream FS=NewFileStream (FileName, FileMode.Create); BinaryWriter BW=NewBinaryWriter (FS); bw. Write (bytes); bw. Close (); Fs. Close (); } five. Read Stream from File PublicStream Filetostream (stringfileName) { //Open FileFileStream FileStream=NewFileStream (FileName, FileMode.Open, FileAccess.Read, fileshare.read);//read the file byte[]byte[] bytes =New byte[filestream.length]; FileStream.Read (bytes,0, Bytes. Length); Filestream.close (); //convert byte[] to StreamStream Stream=NewMemoryStream (bytes);returnstream;}
Conversion between C # Stream and byte[] (application of file stream)