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

一. 二进制转换成图片

MemoryStream ms = new MemoryStream(bytes);

ms.Position = 0;

Image img = Image.FromStream(ms);

ms.Close();

this.pictureBox1.Image

二. C#中byte[]与string的转换代码

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[] 之间的转换

/// 将 Stream 转成 byte[]

public byte[] StreamToBytes(Stream stream)

{

byte[] bytes = new byte[stream.Length];

stream.Read(bytes, 0, bytes.Length);

// 设置当前流的位置为流的开始

stream.Seek(0, SeekOrigin.Begin);

return bytes;

}

/// 将 byte[] 转成 Stream

public Stream BytesToStream(byte[] bytes)

{

Stream stream = new MemoryStream(bytes);

return stream;

}

四. Stream 和 文件之间的转换

将 Stream 写入文件

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();

}

五. 从文件读取 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[] 转换成 Stream

Stream stream = new MemoryStream(bytes);

return stream;

}

C#图像处理:Stream 与 byte[] 相互转换,byte[]与string,Stream 与 File 相互转换等的更多相关文章

  1. Java之byte、char和String类型相互转换

    package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...

  2. [.Net,C#]三类资源:流对象Stream,字节数组byte[],图片Image

    三类资源:流对象Stream,字节数组byte[],图片Image 关系:Stream<=>byte[],byte[]<=>Image Stream 与Image相互转化的媒介 ...

  3. 【C#基础】byte二进制数组转string

    //解析post请求数组返回的数组 //解码返回的二进制数组 public string DecodeBytes(byte[] c) { string html = string.Empty; try ...

  4. short a = 128, byte b = (byte)a 强制类型转换

    package 笔试; public class ShortToByte { /** * @param args */ public static void main(String[] args) { ...

  5. Byte Array to Hexadecimal String

    Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...

  6. Convert a byte[] array to readable string format. This makes the "hex" readable!

    /* * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol ...

  7. 接口请求,上传byte数组byte[]数据异常,负数变正数/负数变63

    一.背景 最近项目中有个需求,就是需要把一个byte[]数组上传到服务端.但是我发现发送的byte数组和服务端接收的数组不一样,所有的正数在传递时正确,数组长度也没变化,但是负数变成了63或者负数全部 ...

  8. Java中如何正确的将byte[]数组转化为String类型?

    很多人在编程时,总是喜欢用一下方法将数组转为字符串:(a为byte数组) String s=a.toString(); 可是每次返回的时候,新手看来返回的结果是乱码,比如说我,写RSA算法时,没有注意 ...

  9. How to convert a byte to its binary string representation

    How to convert a byte to its binary string representation For example, the bits in a byte B are 1000 ...

随机推荐

  1. 【LeetCode】3. 无重复字符的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...

  2. CS229 6.8 Neurons Networks implements of PCA ZCA and whitening

    PCA 给定一组二维数据,每列十一组样本,共45个样本点 -6.7644914e-01  -6.3089308e-01  -4.8915202e-01 ... -4.4722050e-01  -7.4 ...

  3. Linux性能优化 第六章 性能工具:磁盘I/O

    6.1 磁盘I/O介绍 一般来说,Linux磁盘的每个分区要么包含一个文件系统,要么包含一个交换分区.这些分区被挂载到Linux根文件系统,该系统由/etc/fstab指定.这些被挂载的文件系统包含了 ...

  4. EventBus的使用;消息传递之EventBus;

    EventBus传递消息(数据)和广播有点像,对广播传递数据有兴趣的可以看一下:Android数据传递,使用广播BroadcastReceiver: 1.添加build.gradle implemen ...

  5. WPF 获取文件夹路径,目录路径,复制文件,选择下载文件夹/目录

    private void Border_MouseLeftButtonUp_4(object sender, MouseButtonEventArgs e) { //获取项目中文件 , System. ...

  6. MVC Html辅助方法DropDownList的简单使用、连接MYSQL数据库用自定义model类接收

    附上启发链接:https://www.cnblogs.com/CreateMyself/p/5424894.html [HttpGet] public ActionResult Edit(int id ...

  7. jQuery插件——下拉选择框

    其实,之前也写过jQuery插件,今天写的是一个模拟select选择的下拉插件. 既然是jQuery插件,那么必然是依赖jQuery的了. 老规矩,直接上代码吧! ;(function () { $. ...

  8. java中六个时间类的使用和区别

    java.util.Date java.sql.Date   java.sql.Time   java.sql.Timestamp java.text.SimpleDateFormat java.ut ...

  9. JSP 调用java 常量 枚举

    JAVA:public enum ReimStatus { UNCONFIRMED ("118001"), //未确认 DISPATCH_VERIFY("118002&q ...

  10. ROS 进阶学习笔记(13) - Combine Subscriber and Publisher in Python, ROS

    Combine Subscriber and Publisher in Python, ROS This article will describe an example of Combining S ...