字节数组byte[]与图片image之间的转化

字节数组转换成图片

public static Image byte2img(byte[] buffer)
{
    MemoryStream ms = new MemoryStream(buffer);
    ms.Position = 0;
    Image img = Image.FromStream(ms);
    ms.Close();
    return img;
}

图片转化为字节数组

public static byte[] byte2img(Bitmap Bit)
{
    byte[] back = null;
    MemoryStream ms = new MemoryStream();
    Bit.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    back = ms.GetBuffer();
    return back;
}

字节数组byte[]与字符串string之间的编码解码

字符串到字节数组的编码

public static byte[] str2byte(string str)
{
    byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
    //byte[] data = Convert.FromBase64String(param);
    //有很多种编码方式,可参考:http://blog.csdn.net/luanpeng825485697/article/details/77622243
    return data;
}

字节数组到字符串的解码

public static string str2byte(byte[] data)
{
    string str = System.Text.Encoding.UTF8.GetString(data);
    //str = Convert.ToBase64String(data);
    //有很多种编码方式,可参考:http://blog.csdn.net/luanpeng825485697/article/details/77622243
    return str;
}

字节数组byte[]与内存流MemoryStream之间的转换

字节数组转化为输入内存流

public static MemoryStream byte2stream(byte[] data)
{
    MemoryStream inputStream = new MemoryStream(data);
    return inputStream;
}

输出内存流转化为字节数组

public static byte[] byte2stream(MemoryStream outStream)
{
    return outStream.ToArray();
}

字节数组byte[]与流stream之间的转换

将 Stream 转成 byte[]

public byte[] stream2byte(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 byte2stream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}

流Stream 和 文件file之间的转换

将 Stream 写入文件

public void stream2file(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 file2stream(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#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换的更多相关文章

  1. C#中字节数组byte[]和字符串string类型的相互转换

    C#中字节数组byte[]和字符串string类型的相互转换: string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBy ...

  2. C#中字节数组(byte[])和字符串相互转换

    转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...

  3. java中字节数组byte[]和字符(字符串)之间的转换

    转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...

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

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

  5. Java基础知识强化之IO流笔记27:FileInputStream读取数据一次一个字节数组byte[ ]

    1. FileInputStream读取数据一次一个字节数组byte[ ]  使用FileInputStream一次读取一个字节数组: int read(byte[]  b) 返回值:返回值其实是实际 ...

  6. 字节数组byte[]和整型,浮点型数据的转换——Java代码

    近期在写C++ socket和java socket之间的通信程序,涉及到整数浮点数的传输.须要从字节数组还原数据,查了一些资料.总结例如以下 1.       整数和浮点数的机器表示 在机器内部.不 ...

  7. C#-----字节数组(byte[])和字符串相互转换

       Encoding类  表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System ...

  8. 【stanford C++】字符串(String)与流(Stream)

    字符串(String)与流(Stream) 一.C++中字符串(String) 字符串(String):就是(可能是空的)字符序列. C++中的字符串在概念上和Java中的字符串类似. C++字符串用 ...

  9. Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

随机推荐

  1. 201521123085 《Java程序设计》 第2周学习总结

    1. 本周学习总结 1.学习了string类:   2.java数组的使用:   3.学习了类名包名. 2. 书面作业 Q1.使用Eclipse关联jdk源代码,并查看String对象的源代码(截图) ...

  2. Java:验证在类继承过程中equals()、 hashcode()、toString()方法的使用

    以下通过实际例子对类创建过程汇中常用的equals().hashcode().toString()方法进行展示,三个方法的创建过程具有通用性,在项目中可直接改写. //通过超类Employee和其子类 ...

  3. request.getParameter()获取URL中文参数乱码的解决办法

    这个问题耽误好长时间,URL传中文参数出现乱码,就算首次使用request接收就添加 request.setCharacterEncoding("UTf-8"); 依然报错不误. ...

  4. 二分求最长上升子序列 二分LIS

    #include <iostream> #include <cstring> #define N 50010 using namespace std; int n; int n ...

  5. Oracle总结第三篇【PLSQL】

    PLSQL介绍 PLSQL是Oracle对SQL99的一种扩展,基本每一种数据库都会对SQL进行扩展,Oracle对SQL的扩展就叫做PLSQL- SQL99是什么 (1)是操作所有关系型数据库的规则 ...

  6. Hibernate逆向工程【PowerDesigner、idea环境下】

    为什么要使用逆向工程 由于我们每次编写Hibernate的时候都需要写实体,写映射文件.而且Hibernate的映射文件也容易出错.而逆向工程可以帮我们自动生成实体和映射文件,这样就非常方便了. 使用 ...

  7. NDK调试

    第一种(控制台输出): 1.配置好环境变量,这是为了方便起见.将你sdk和ndk的根目录放到环境变量path中.配置完成之后可以来个小检测: 在命令行分别输入adb和ndk-stack后点击回车,只要 ...

  8. 用vue开发一个app(2,main.js)

    昨天跟着vue的官网搭建了vue的一个脚手架,我也是第一次用VUE一切都在摸索阶段. 今天试着看下里面脚手架里面有点什么东西 先看看main.js 导入了3个模块 一个vue,一个app,还有rout ...

  9. ios开发——实用技术篇&三维旋转动画

    实现三位旋转动画的方法有很多种,这里介绍三种 一:UIView 1 [UIView animateWithDuration:1.0 animations:^{ 2 self.iconView.laye ...

  10. 西邮linux兴趣小组2014纳新免试题(三)

    [第三关] 题目 http://sortsth.sinaapp.com/ 分析 查看网页源码,得知题目让找出6种排序算法,每次刷新或提交序列都变化. 15种算法清单: CountingSort     ...