Conversion between C # Stream and byte[] (application of file stream)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.