国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實例教程

2020-01-24 02:27:29
字體:
供稿:網(wǎng)友

本文以實例形式詳細介紹了C#實現(xiàn)Stream與byte[]之間的轉(zhuǎn)換的方法,分享給大家供大家參考之用。具體方法如下:

一、二進制轉(zhuǎn)換成圖片

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

二、C#中byte[]與string的轉(zhuǎn)換代碼

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

三、C# Stream 和 byte[] 之間的轉(zhuǎn)換

1.將 Stream 轉(zhuǎn)成 byte[] 

public byte[] StreamToBytes(Stream stream) {   byte[] bytes = new byte[stream.Length];   stream.Read(bytes, 0, bytes.Length);   // 設(shè)置當(dāng)前流的位置為流的開始   stream.Seek(0, SeekOrigin.Begin);   return bytes; } 

2.將 byte[] 轉(zhuǎn)成 Stream 

public Stream BytesToStream(byte[] bytes) {   Stream stream = new MemoryStream(bytes);   return stream; }

四、Stream 和 文件之間的轉(zhuǎn)換

將 Stream 寫入文件

public void StreamToFile(Stream stream,string fileName) {   // 把 Stream 轉(zhuǎn)換成 byte[]   byte[] bytes = new byte[stream.Length];   stream.Read(bytes, 0, bytes.Length);   // 設(shè)置當(dāng)前流的位置為流的開始   stream.Seek(0, SeekOrigin.Begin);   // 把 byte[] 寫入文件   FileStream fs = new FileStream(fileName, FileMode.Create);   BinaryWriter bw = new BinaryWriter(fs);   bw.Write(bytes);   bw.Close();   fs.Close(); }

五、從文件讀取 Stream

public Stream FileToStream(string fileName) {         // 打開文件   FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);   // 讀取文件的 byte[]   byte[] bytes = new byte[fileStream.Length];   fileStream.Read(bytes, 0, bytes.Length);   fileStream.Close();   // 把 byte[] 轉(zhuǎn)換成 Stream   Stream stream = new MemoryStream(bytes);   return stream; }

六、Bitmap 轉(zhuǎn)化為 Byte[]

Bitmap BitReturn = new Bitmap(); byte[] bReturn = null; MemoryStream ms = new MemoryStream(); BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png); bReturn = ms.GetBuffer();

相信本文所述對大家的C#程序設(shè)計有一定的借鑒價值。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 三河市| 平湖市| 航空| 巩义市| 广德县| 瓮安县| 江都市| 延长县| 阳西县| 鄂温| 德惠市| 利津县| 天长市| 鲁山县| 固始县| 婺源县| 石门县| 大邑县| 邵阳县| 东兰县| 霍邱县| 璧山县| 井冈山市| 怀安县| 新建县| 阿克陶县| 昆山市| 韶关市| 临颍县| 金阳县| 长春市| 大邑县| 富顺县| 彭州市| 九江市| 天水市| 贺州市| 龙里县| 渝中区| 江油市| 天等县|