Conversion between stream and byte (from www. sysoft. CC)

Source: Internet
Author: User
Conversion between C # stream and byte []

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

Conversion between stream and byte (from www. sysoft. CC)

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.