c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                字節(jié)數(shù)組byte[]與圖片image之間的轉(zhuǎn)化
字節(jié)數(shù)組轉(zhuǎn)換成圖片
public static Image byte2img(byte[] buffer) {MemoryStream ms = new MemoryStream(buffer);ms.Position = 0;Image img = Image.FromStream(ms);ms.Close();return img; }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 
圖片轉(zhuǎn)化為字節(jié)數(shù)組
public static byte[] byte2img(Bitmap Bit) {byte[] back = null;MemoryStream ms = new MemoryStream();Bit.Save(ms, System.Drawing.Imaging.ImageFormat.Png);back = ms.GetBuffer();return back; }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 
字節(jié)數(shù)組byte[]與字符串string之間的編碼解碼
字符串到字節(jié)數(shù)組的編碼
public static byte[] str2byte(string str) {byte[] data = System.Text.Encoding.UTF8.GetBytes(param);//byte[] data = Convert.FromBase64String(param);//有很多種編碼方式,可參考:http://blog.csdn.net/luanpeng825485697/article/details/77622243return data; }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 
字節(jié)數(shù)組到字符串的解碼
public static string str2byte(byte[] data) {string str = System.Text.Encoding.UTF8.GetString(data);//str = Convert.ToBase64String(data);//有很多種編碼方式,可參考:http://blog.csdn.net/luanpeng825485697/article/details/77622243return str; }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 
字節(jié)數(shù)組byte[]與內(nèi)存流MemoryStream之間的轉(zhuǎn)換
字節(jié)數(shù)組轉(zhuǎn)化為輸入內(nèi)存流
public static MemoryStream byte2stream(byte[] data) {MemoryStream inputStream = new MemoryStream(data);return inputStream; }- 1
 - 2
 - 3
 - 4
 - 5
 
輸出內(nèi)存流轉(zhuǎn)化為字節(jié)數(shù)組
public static byte[] byte2stream(MemoryStream outStream) {return outStream.ToArray(); }- 1
 - 2
 - 3
 - 4
 
字節(jié)數(shù)組byte[]與流stream之間的轉(zhuǎn)換
將 Stream 轉(zhuǎn)成 byte[]
public byte[] stream2byte(Stream stream) {byte[] bytes = new byte[stream.Length];stream.Read(bytes, 0, bytes.Length);// 設(shè)置當(dāng)前流的位置為流的開(kāi)始stream.Seek(0, SeekOrigin.Begin);return bytes; }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 
將 byte[] 轉(zhuǎn)成 Stream
public Stream byte2stream(byte[] bytes) {Stream stream = new MemoryStream(bytes);return stream; }- 1
 - 2
 - 3
 - 4
 - 5
 
流Stream 和 文件file之間的轉(zhuǎn)換
將 Stream 寫(xiě)入文件
public void stream2file(Stream stream,string fileName) {// 把 Stream 轉(zhuǎn)換成 byte[]byte[] bytes = new byte[stream.Length];stream.Read(bytes, 0, bytes.Length);// 設(shè)置當(dāng)前流的位置為流的開(kāi)始stream.Seek(0, SeekOrigin.Begin);// 把 byte[] 寫(xiě)入文件FileStream fs = new FileStream(fileName, FileMode.Create);BinaryWriter bw = new BinaryWriter(fs);bw.Write(bytes);bw.Close();fs.Close(); }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 
從文件讀取 Stream
public Stream file2stream(string fileName) { // 打開(kāi)文件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)換成 StreamStream stream = new MemoryStream(bytes);return stream; }- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 
總結(jié)
以上是生活随笔為你收集整理的c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: H264基本概念之 宏块、片和片组
 - 下一篇: C++ 如何用创建txt文件,并且写入内