日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

C#二进制格式与文件相互转换

發(fā)布時(shí)間:2025/5/22 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#二进制格式与文件相互转换 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

將文件轉(zhuǎn)換成二進(jìn)制方法:

???? /// <summary>
??? /// 將文件轉(zhuǎn)換成二進(jìn)制
??? /// </summary>
??? /// <param name="Path">文件路徑</param>
??? /// <returns></returns>
??? public static byte[] ConvertToBinary(string Path)
??? {
??????? FileStream stream = new FileInfo(Path).OpenRead();
??????? byte[] buffer = new byte[stream.Length];
??????? stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
??????? return buffer;
??? }

?

將二進(jìn)制轉(zhuǎn)換成文件方法:

using System.IO;

?

string filePath = "D:\\a.jpg";?? //文件路徑
byte[] br = ConvertToBinary(filePath);
FileStream fstream = File.Create(filePath, br.Length);
try
{??
?????fstream.Write(br, 0, br.Length);?? //二進(jìn)制轉(zhuǎn)換成文件
}
catch(Exception ex)
{
?????//拋出異常信息
}
?finally
{
?????fstream.Close();????
}?

?

?

附加:二進(jìn)制與圖片互轉(zhuǎn)

??? Image aa = new Bitmap(@"E:\photo\tm.jpg");

????System.IO.MemoryStream stream
= new System.IO.MemoryStream();
?????System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter?
???? = new?? System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, aa); //將圖像序列化成二進(jìn)制流
????????????????????????
??????????? stream.Position
= 0;
??????????? Image bb
= (Image)formatter.Deserialize(stream);? //將二進(jìn)制流序列成Image
????????????
??

轉(zhuǎn)載于:https://www.cnblogs.com/lmjob/archive/2008/10/07/1305638.html

總結(jié)

以上是生活随笔為你收集整理的C#二进制格式与文件相互转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。