zoukankan      html  css  js  c++  java
  • C#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

    c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

      1 c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
      2 
      3 /*********字节数组byte[]与图片image之间的转化**********/
      4 //字节数组转换成图片
      5 public static Image byte2img(byte[] buffer)
      6 {
      7     MemoryStream ms = new MemoryStream(buffer);
      8     ms.Position = 0;
      9     Image img = Image.FromStream(ms);
     10     ms.Close();
     11     return img;
     12 }
     13 
     14 
     15 //图片转化为字节数组
     16 public static byte[] byte2img(Bitmap Bit)
     17 {
     18     byte[] back = null;
     19     MemoryStream ms = new MemoryStream();
     20     Bit.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
     21     back = ms.GetBuffer();
     22     return back;
     23 }
     24 
     25 
     26 /**********字节数组byte[]与字符串string之间的编码解码*****/
     27 //字符串到字节数组的编码
     28 public static byte[] str2byte(string str)
     29 {
     30     byte[] data = System.Text.Encoding.UTF8.GetBytes(param);
     31     //byte[] data = Convert.FromBase64String(param);
     32     //有很多种编码方式,可参考:http://blog.csdn.net/luanpeng825485697/article/details/77622243
     33     return data;
     34 }
     35 
     36 
     37 //字节数组到字符串的解码
     38 public static string str2byte(byte[] data)
     39 {
     40     string str = System.Text.Encoding.UTF8.GetString(data);
     41     //str = Convert.ToBase64String(data);
     42     //有很多种编码方式,可参考:http://blog.csdn.net/luanpeng825485697/article/details/77622243
     43     return str;
     44 }
     45 
     46 /****字节数组byte[]与内存流MemoryStream之间的转换****/
     47 //字节数组转化为输入内存流
     48 public static MemoryStream byte2stream(byte[] data)
     49 {
     50     MemoryStream inputStream = new MemoryStream(data);
     51     return inputStream;
     52 }
     53 
     54 
     55 //输出内存流转化为字节数组
     56 public static byte[] byte2stream(MemoryStream outStream)
     57 {
     58     return outStream.ToArray();
     59 }
     60 
     61 /************字节数组byte[]与流stream之间的转换********/
     62 //将 Stream 转成 byte[]
     63 public byte[] stream2byte(Stream stream)
     64 {
     65     byte[] bytes = new byte[stream.Length];
     66     stream.Read(bytes, 0, bytes.Length);
     67     // 设置当前流的位置为流的开始
     68     stream.Seek(0, SeekOrigin.Begin);
     69     return bytes;
     70 }
     71 
     72 
     73 //将 byte[] 转成 Stream
     74 public Stream byte2stream(byte[] bytes)
     75 {
     76     Stream stream = new MemoryStream(bytes);
     77     return stream;
     78 }
     79 
     80 流Stream 和 文件file之间的转换
     81 
     82 //将 Stream 写入文件
     83 public void stream2file(Stream stream,string fileName)
     84 {
     85     // 把 Stream 转换成 byte[]
     86     byte[] bytes = new byte[stream.Length];
     87     stream.Read(bytes, 0, bytes.Length);
     88     // 设置当前流的位置为流的开始
     89     stream.Seek(0, SeekOrigin.Begin);
     90     // 把 byte[] 写入文件
     91     FileStream fs = new FileStream(fileName, FileMode.Create);
     92     BinaryWriter bw = new BinaryWriter(fs);
     93     bw.Write(bytes);
     94     bw.Close();
     95     fs.Close();
     96 }
     97 
     98 
     99 //从文件读取 Stream
    100 public Stream file2stream(string fileName)
    101 {            
    102     // 打开文件
    103     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    104     // 读取文件的 byte[]
    105     byte[] bytes = new byte[fileStream.Length];
    106     fileStream.Read(bytes, 0, bytes.Length);
    107     fileStream.Close();
    108     // 把 byte[] 转换成 Stream
    109     Stream stream = new MemoryStream(bytes);
    110     return stream;
    111 }
  • 相关阅读:
    各数据库连接maven配置
    在 windows 下配置 git 公共库
    java笔记2(观察者模式、内部类、输入输出)
    linux 常用命令的应用笔记
    Setting Up Google Code And Github With Git
    java笔记3(单元测试中的模式)
    Apache的配置
    Ubuntu安装常用工具
    MongoDB笔记1(安装配置、用户管理、查询与SQL比较)
    Redis的安装与配置
  • 原文地址:https://www.cnblogs.com/hbtmwangjin/p/9143835.html
Copyright © 2011-2022 走看看