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 = 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; } 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 = 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; } |