Conversion between Stream and byte[]

Source: Internet
Author: User

1. Binary conversion to Picture

New0= Image.fromstream (ms); Ms. Close ();

2.c# in byte[] and string conversion Code

1):
var New System.Text.UnicodeEncoding (); byte [] Inputbytes = converter. GetBytes (""); string 0, inputbytes.length);
2):
String inputstring = System.Convert.ToBase64String (inputbytes);
byte[] inputbytes = System.Convert.FromBase64String (inputstring);
FileStream FileStream = new FileStream (FileName, FileMode.Open, FileAccess.Read, FileShare.Read);

3. Conversion between C # Stream and 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;}

Conversion between 4.Stream and files

 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 ();}
5. Reading Stream from a 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;}
6.Bitmap conversion to byte[]
New Bitmap (); byte NULL  new= Ms. GetBuffer ();

Conversion between Stream and byte[]

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.