/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 写入文件
/// </summary>
public void StreamToFile(Stream stream,string fileName)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
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)
{
// 打开文件
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[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
分享到:
相关推荐
将图片由bitmap类型转换为stream类型再转换为byte[]类型,然后再还原
Stream 和 byte[] 之间的转换
本文以实例形式详细介绍了C#实现Stream与byte[]之间的转换的方法,分享给大家供大家参考之用。具体方法如下: 一、二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = ...
它为搜索模式和数据转换提供了许多有用的功能。 为尽可能快的数据处理进行了优化。 方法描述清除清除现有流fromArrayBuffer 从现有的“ ArrayBuffer”初始化“ Stream”对象fromUint8Array 从现有的“ Uint8Array”...
C#将图片和字节流相互转换代码: usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Drawing; usingSystem.IO; namespaceMicrosoft.Form.Base { classImageToByte {...
图片转二进制,二进制转图片,用XML保存二进制图片
代码如下:(1)byte[] bt=System.Text.Encoding.Default.GetBytes(“字符串”); (2)byte[] bt=Convert.FromBase64String(“字符串”); 2.字符串转流 代码如下:(1)MemoryStream ms=new MemoryStream(System.Text....
1.参数是Byte[]类型,返回值是Image对象: public System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = ...
文本字符串与byte[], 整个物理文件与byte[], 物理文件内容与byte[], stream与byty[] 之间的相互转换。 从磁盘读取文件,将文件写入磁盘。
//主要通过Stream作为中间桥梁 public static Image ByteArrayToImage(byte[] iamgebytes) { MemoryStream ms = new MemoryStream(iamgebytes); Image image = Image.FromStream(ms); return image; } public ...
一个封装好的文件操作的例子 Stream、byte[] 和 文件之间的转换
本文实例讲述了C#将字节数组转换成数字的方法。分享给大家供大家参考。具体实现方法如下: // Create a decimal from a byte array public static decimal ByteArrayToDecimal (byte[] src) { // Create a ...
二进制流替换用于 Node 和浏览器的转换流,可有效替换二进制流中的字节序列。 可以限制为仅替换前 n 个出现。用法 var ReplaceStream = require ( 'binary-stream-replace' ) ;// replace first 10 occurrences of ...
本文实例讲述了C#将数字转换成字节数组的方法。分享给大家供大家参考。具体实现方法如下: 下面的代码用到了MemoryStream 和 BinaryWriter // Create a byte array from a decimal public static byte[] ...
涉及知识点(dll文件生成,应用调用dll与动态载入dll,socket网络通信,int,string,Stream与byte数组间的相互转换,图片处理技术等)代码较多,但基本都有注释,看起来容易理解,有问题请联系QQ:1207651505(加备注)
ASP.NET中在判断文件格式时,我们以前常用的方法就是通过截取扩展名来做判断,或者通过ContentType (MIME) 判断,这两种方法都不太安全,因为... //转换成byte,读取图片MIME类型 Stream stream; //int contentLength
var batcher = require ( 'byte-stream' ) var daffStream = require ( 'daff-stream' ) var older = from . obj ( { key : 1 , value : { 'a' : 1 , 'b' : 2 , 'c' : 3 } ) var newer = from . obj ( { key : 1 , ...
Using the ODX runtime data information, the D-Server converts the application's request into a byte stream, which is called a diagnostic protocol data unit (D-PDU). The D-PDU is handed over to the ...