C # conversion between Stream and byte,

Source: Internet
Author: User

C # conversion between Stream and byte,

I. BinaryConversionImage Creation

 

MemoryStream ms = new MemoryStream(bytes);ms.Position = 0;Image img = Image.FromStream(ms);ms.Close();this.pictureBox1.Image

 

2. in C #, byte [] and stringConversionCode

 

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);

 

III. C # Between Stream and byte []Conversion

 

/// Convert Stream to byte [] public byte [] StreamToBytes (Stream stream) {byte [] bytes = new byte [stream. length]; stream. read (bytes, 0, bytes. length); // set the current stream position to the starting stream of the stream. seek (0, SeekOrigin. begin); return bytes;} // convert byte [] to Streampublic Stream BytesToStream (byte [] bytes) {Stream stream = new MemoryStream (bytes); return stream ;}

 

4. Stream and fileConversion

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 starting stream of the stream. seek (0, SeekOrigin. begin); // write byte [] to the 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 fileStream = new FileStream (fileName, FileMode. open, FileAccess. read, FileShare. read); // Read the byte [] byte [] bytes = new byte [fileStream. length]; fileStream. read (bytes, 0, bytes. length); fileStream. close (); // convert byte [] to Stream stream = new MemoryStream (bytes); return stream ;}

 

Sat.

 

// Bitmap converted to Byte [] Bitmap BitReturn = new Bitmap (); byte [] bReturn = null; MemoryStream MS = new MemoryStream (); BitReturn. save (MS, System. drawing. imaging. imageFormat. png); bReturn = ms. getBuffer ();

Related Article

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.