C# Stream 和 byte[] 之間的轉(zhuǎn)換
更新時(shí)間:2008年03月09日 20:09:18 作者:
Stream 和 byte[] 之間的轉(zhuǎn)換
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之間的轉(zhuǎn)換
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 將 Stream 轉(zhuǎn)成 byte[]
/// </summary>
public byte[] StreamToBytes(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;
}
/// <summary>
/// 將 byte[] 轉(zhuǎn)成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之間的轉(zhuǎn)換
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 將 Stream 寫入文件
/// </summary>
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)前流的位置為流的開(kāi)始
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();
}
/// <summary>
/// 從文件讀取 Stream
/// </summary>
public Stream FileToStream(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)換成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
* Stream 和 byte[] 之間的轉(zhuǎn)換
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 將 Stream 轉(zhuǎn)成 byte[]
/// </summary>
public byte[] StreamToBytes(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;
}
/// <summary>
/// 將 byte[] 轉(zhuǎn)成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之間的轉(zhuǎn)換
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 將 Stream 寫入文件
/// </summary>
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)前流的位置為流的開(kāi)始
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();
}
/// <summary>
/// 從文件讀取 Stream
/// </summary>
public Stream FileToStream(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)換成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
相關(guān)文章
visio二次開(kāi)發(fā)--判斷文檔是否已發(fā)生變化(變化就加星號(hào)*)
最近做一個(gè)故障樹(shù)診斷的項(xiàng)目,用visio二次開(kāi)發(fā),可以同時(shí)打開(kāi)多個(gè)繪制的故障樹(shù)圖形文檔。項(xiàng)目中需要實(shí)現(xiàn)判斷文檔是否發(fā)生變化,這是很多編輯軟件的基本功能,變化了就加個(gè)星號(hào)*2013-04-04
C#連接Oracle數(shù)據(jù)庫(kù)字符串(引入DLL)的方式
這篇文章主要給大家介紹了關(guān)于C#連接Oracle數(shù)據(jù)庫(kù)字符串(引入DLL)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用
這篇文章主要給大家介紹了關(guān)于C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
C#?Windows?Forms中實(shí)現(xiàn)控件之間的連接線的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在C#?Windows?Forms應(yīng)用程序中實(shí)現(xiàn)繪圖工具中多個(gè)控件之間的連接線功能,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02

