Conversion between C # stream and byte []

Source: Internet
Author: User

I.Convert binary to image
Memorystream MS = new memorystream (bytes );
Ms. Position = 0;
Image IMG = image. fromstream (MS );
Ms. Close ();
This. picturebox1.image

II.Conversion between byte [] and string in C #Code

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

4.Stream and file conversion

Write stream to file

Public void streamtofile (Stream stream, string filename)
{< br> // converts stream to byte []
byte [] bytes = new byte [stream. length];
stream. read (bytes, 0, bytes. length );
// set the starting position of the current 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 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;
}

 

Reprinted from: http://www.myext.cn/csharp/6415.html

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.