Conversion between C # Stream and byte[]

Source: Internet
Author: User

Overview

This article mainly explains the conversion between stream and byte[].

Conversion between Stream and byte[]

<summary>///stream to byte[]///</summary>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);    

Byte[] turn into Stream

<summary>///byte[] to stream///</summary>public stream bytestostream (byte[] bytes) {    stream stream = new MemoryStream (bytes);    

Conversion between Stream and file

<summary>///writes stream to file//</summary>public void Streamtofile (Stream stream,string fileName) {    // Convert 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 ();    

Read Stream from File

<summary>///read from file stream///</summary>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);    

Summarize

stream pictures are stored in a data stream format, converted to byte[] format and stored in a database .


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Conversion between C # 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.