`
wyf
  • 浏览: 433576 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
社区版块
存档分类
最新评论

Stream to Byte[]

 
阅读更多
/* - - - - - - - - - - - - - - - - - - - - - - - - 
 * 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;
}

 

分享到:
评论

相关推荐

    C#中bitmap、stream、byte类型转换实例

    在处理图像时,我们可能会遇到需要在不同的数据类型之间转换的情况,比如从Bitmap到Stream,再到byte数组,最后再还原回Bitmap。这样的转换在上传图片、存储图片数据或在网络中传输时非常常见。本篇文章将详细介绍...

    C# Stream 和 byte[] 之间的转换

    public Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // 读取文件的 byte[] byte[] bytes = new...

    图片转换格式(byte[],Stream,string)

    Stream imageStream = FileToStream("path/to/image.jpg"); // Stream转byte[] byte[] imageData = StreamToBytes(imageStream); // byte[]转Base64字符串 string base64Image = Convert.ToBase64String...

    c#实现object与byte[]互转

    例如,可以使用 Convert.ToByte() 方法将object转换为byte[],但是这只适用于简单的对象,例如字符串。 将object转换为byte[]需要选择合适的序列化方式, BinaryFormatter 和 JsonSerializer 是两个常用的选择。

    C#实现Stream与byte[]之间的转换实例教程

    在C#编程中,Stream和byte[]是两种常见的数据处理对象。Stream代表了一种抽象的数据流,它可以是内存中的数据、硬盘上的文件或是网络传输的数据。而byte[]则是一个字节数组,常用于存储二进制数据。两者之间进行转换...

    DataSet 与Byte[]之间的相互转换

    这个文件可能提供了将`DataSet`转换为`Stream`对象的示例,因为`Byte[]`可以通过`MemoryStream`类表示为流。在将`DataSet`转换为`Stream`后,可以方便地应用数据压缩技术,然后再将结果转换回`Byte[]`。文件中可能...

    C# 图片转成byte存入,数据库读取byte转成图片的类

    读取数据库的byte数组后,我们可以再次使用`Image`类的`FromStream()`方法将它还原为图片对象: ```csharp public Image ByteArrayToImage(byte[] imageData) { using (MemoryStream ms = new MemoryStream...

    Blob、InputStream、byte 互转

    private byte[] inputStreamToByte(InputStream is) throws IOException { ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { bytestream.write(ch)...

    ADO Stream 数据库存储图片文件

    imageBytes = new byte[(int)adoStream.Size]; adoStream.Read(imageBytes, 0, (int)adoStream.Size); // 将imageBytes转换回图片并显示 } } ``` 这个过程概括了如何利用ADO Stream流对象在数据库中存储和检索...

    image与字节数组byte的互转

    在计算机编程中,图像数据通常以二进制形式存储,其中最常见的表示方式是字节数组(byte array)。在处理图像时,有时我们需要将图像文件转换为字节数组,以便于在网络上传输或者存储在数据库中。反之,我们也可能...

    文字转成byte[],再转成string加密,再解密转成byte[] C#

    此外,考虑到加解密的效率和安全性,还可以考虑使用流式加密(Stream-based encryption)来处理大数据量的传输。 在实际开发中,可以封装这些操作为一个类或方法,方便在多个地方复用。对于更复杂的场景,可能还...

    Java 类型相互转换byte[]类型,Blob类型详细介绍

    private byte[] inputStreamToByte(InputStream is) throws IOException { ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { bytestream.write(ch);...

    实现内存Stream压缩解压缩 JPG 图像

    Dim compressedBytes As Byte() = memoryStream.ToArray() File.WriteAllBytes("compressed.jpg", compressedBytes) ``` 解压缩的过程与压缩类似,但需要反转步骤: 1. **读取压缩数据**:从文件中读取压缩的图像...

    鲜花网站建设

    toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh) Else toByte = toByte & chrB(AscB(c)) End If Next End function End Class Class FileInfo dim FormName,FileName,FilePath,FileSize,FileStart ...

    使用Stream读写存储的图像

    2. **将图片数据转换为Blob**:有了包含图片数据的MemoryStream后,可以将其转换为字节数组(Byte[]),这是大多数数据库系统支持的数据类型。使用MemoryStream的ToArray方法即可完成转换。 3. **存储到Access**:...

    C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换

    本文将详细探讨如何在字符串(string)、内存流(MemoryStream)和比特数组(byte[])之间进行转换,这对于数据存储、网络传输或者序列化/反序列化等场景非常有用。 1. **字符串转比特数组**: - `System.Text.Encoding....

    C# FileStream 分段读取文本内容

    `FileStream`是`Stream`类的一个具体实现,`Stream`类是所有输入/输出流的基础抽象类。下面我们将详细探讨如何使用`FileStream`分段读取文本内容。 首先,创建一个`FileStream`对象需要提供文件路径和访问模式。...

    C#中图片.BYTE[]和base64string的转换方法

    在C#编程中,处理图像数据时经常需要在不同的数据格式之间进行转换,例如将图片文件转换为字节数组(BYTE[])或Base64编码的字符串(base64string)。这种转换对于在网络上传输图像数据,或者在数据库中存储和检索图像...

    manual-merge-stream:使用用户输入合并排序差异流

    var Batcher = require ( 'byte-stream' ) var manualMergeStream = require ( 'manual-merge-stream' ) var diffs2string = require ( 'diffs-to-string' ) var limit = 5 var batchStream = Batcher ( limit ) ...

    详谈C# 图片与byte[]之间以及byte[]与string之间的转换

    本文将深入探讨如何在C#中实现图片(Image类型)与字节数组(byte[]类型)以及字节数组与字符串(string类型)之间的转换。 1. 图片(Image)与字节数组(byte[])的转换: - **byte[]到Image的转换**:这个过程...

Global site tag (gtag.js) - Google Analytics