Example tutorial for converting between stream and byte[] in C #

Source: Internet
Author: User

This paper introduces in detail the method of converting C # between stream and byte[in the form of instance, and shares it for everyone's reference. Here's how:


One or two conversion into pictures

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


Conversion code of 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 FileStream = new FileStream (FileName, FileMode.Open, FileAccess.Read, FileShare.Read);


Iii. conversion between C # Stream and byte[]


1. Turn the Stream into byte[]

Public byte[] Streamtobytes (Stream stream) {  byte[] bytes = new Byte[stream. Length];  Stream. Read (bytes, 0, bytes. Length);  Sets the current stream's position as the stream's start stream  . Seek (0, seekorigin.begin);  return bytes;}


2. Turn byte[] into Stream

Public stream Bytestostream (byte[] bytes) {  Stream stream = new MemoryStream (bytes);  return stream;}


Iv. conversion between Stream and file


Write Stream to File

public void Streamtofile (stream stream,string fileName) {  //converts stream to byte[]  byte[] bytes = new Byte[stream. Length];  Stream. Read (bytes, 0, bytes. Length);  Sets the current stream's position as the stream's start stream  . Seek (0, seekorigin.begin);  Write byte[] to file  FileStream fs = new FileStream (FileName, filemode.create);  BinaryWriter bw = new BinaryWriter (fs);  Bw. Write (bytes);  Bw. Close ();  Fs. Close ();}


V. Reading Stream from a file

Public Stream Filetostream (string fileName) {  //Open file  FileStream FileStream = new FileStream (FileName, FileMode.Open, FileAccess.Read, fileshare.read);  Read file byte[]  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;}


Vi. Bitmap conversion to byte[]

Bitmap Bitreturn = new Bitmap (); byte[] Breturn = null; MemoryStream ms = new MemoryStream (); Bitreturn.save (MS, System.Drawing.Imaging.ImageFormat.Png); Breturn = Ms. GetBuffer ();

It is believed that this article has certain reference value for the C # program design of everybody.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Example tutorial for converting between stream and byte[] in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23567.html






Related content C # file download instance code (for each browser) a simple way to connect a database and read fields in a database by C # programs summarize in-depth parsing of generic delegates in C # programming using the foreach statement in C # to iterate through an array and use the array as a parameter
C # Methods for reading configuration files summary C # Methods of encrypting large files with DES encryption algorithm C # namespace and Java Package differences Analysis C # four-bit random number generation method for login verification code

Example tutorial for converting between stream and byte[] in C #

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.