C#實現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實例教程
本文以實例形式詳細介紹了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è)計有一定的借鑒價值。
- 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實例詳解
相關(guān)文章
Unity的IPreprocessBuild實用案例深入解析
這篇文章主要為大家介紹了Unity的IPreprocessBuild實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解
這篇文章主要介紹了unity中實現(xiàn)Edge瀏覽器鼠標(biāo)手勢的功能思路詳解,實現(xiàn)起來其實并不復(fù)雜,涉及的技術(shù)點有pc端和移動端屏幕拖動事件,二維向量的相關(guān)運算,手勢匹配算法,事件系統(tǒng)設(shè)計模式,需要的朋友可以參考下2023-12-12
C#中Convert.ToString和ToString的區(qū)別分析
這篇文章主要介紹了C#中Convert.ToString和ToString的區(qū)別,是C#初學(xué)者需要牢固掌握的技巧,需要的朋友可以參考下2014-08-08
C#程序調(diào)用C++動態(tài)庫(dll文件)遇到的坑及解決
這篇文章主要介紹了C#程序調(diào)用C++動態(tài)庫(dll文件)遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
C#學(xué)習(xí)基礎(chǔ)概念二十五問續(xù)2
C#學(xué)習(xí)基礎(chǔ)概念二十五問續(xù)2...2007-04-04

