C#實(shí)現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實(shí)例教程
本文以實(shí)例形式詳細(xì)介紹了C#實(shí)現(xiàn)Stream與byte[]之間的轉(zhuǎn)換的方法,分享給大家供大家參考之用。具體方法如下:
一、二進(jì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è)計有一定的借鑒價值。
- C# Stream 和 byte[] 之間的轉(zhuǎn)換
- C# 字符串string和內(nèi)存流MemoryStream及比特數(shù)組byte[]之間相互轉(zhuǎn)換
- C# byte數(shù)組與Image相互轉(zhuǎn)換的方法
- C#中Byte[]和String之間轉(zhuǎn)換的方法
- C#中string與byte[]的轉(zhuǎn)換幫助類-.NET教程,C#語言
- C#中兩個byte如何相加
- C#中圖片.BYTE[]和base64string的轉(zhuǎn)換方法
- C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)
- C#如何從byte[]中直接讀取Structure實(shí)例詳解
相關(guān)文章
C#中TreeView節(jié)點(diǎn)的自定義繪制方法
這篇文章主要介紹了C#中TreeView節(jié)點(diǎn)的自定義繪制方法,實(shí)例展示了TreeView節(jié)點(diǎn)的操作技巧,需要的朋友可以參考下2015-02-02
C# ManagementObjectSearcher操作window案例詳解
這篇文章主要介紹了C# ManagementObjectSearcher操作window案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C#使用ADO.Net連接數(shù)據(jù)庫與DbProviderFactory實(shí)現(xiàn)多數(shù)據(jù)庫訪問
這篇文章介紹了C#使用ADO.Net連接數(shù)據(jù)庫與DbProviderFactory實(shí)現(xiàn)多數(shù)據(jù)庫訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C# WinForm調(diào)用Shell_NotifyIcon的示例代碼
這篇文章主要介紹了C# WinForm調(diào)用Shell_NotifyIcon的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11
C# TabControl控件中TabPage選項(xiàng)卡切換時的觸發(fā)事件問題
這篇文章主要介紹了C# TabControl控件中TabPage選項(xiàng)卡切換時的觸發(fā)事件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
C#操作Clipboard讀取剪切板中數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了C#操作Clipboard讀取剪切板中數(shù)據(jù)的方法,實(shí)例分析了C#讀取剪貼板數(shù)據(jù)的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05

