BufferHelp byte[] Stream string FileStream Image Bitmap

  1 /*******    *    ***    *****     **        **
  2  *          *   * *      *    *     *      *
  3  *****      *  *  *      *    *      *    *
  4  *          * *   *      *    *       *  *    
  5  *******   ***    *     *****          */
  6 
  7 using System.Drawing;
  8 using System.IO;
  9 using System.Text;
 10 
 11 namespace Endv
 12 {
 13     //BufferHelp
 14     public static class Buffer
 15     {
 16         /// <summary>
 17         /// 拼接 byte
 18         /// </summary>
 19         /// <param name="buffer1"></param>
 20         /// <param name="buffer2"></param>
 21         /// <returns></returns>
 22         public static byte[] Joint(byte[] buffer1, byte[] buffer2)
 23         {
 24             byte[] bufferWhole = new byte[buffer1.Length + buffer2.Length]; 
 25             System.Buffer.BlockCopy(buffer1, 0, bufferWhole, 0, buffer1.Length); 
 26             System.Buffer.BlockCopy(buffer2, 0, bufferWhole, buffer1.Length, buffer2.Length);
 27             return bufferWhole;
 28         }
 29 
 30         public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3)
 31         {
 32             return Joint(Joint(buffer1, buffer2), buffer3);
 33         }
 34 
 35         public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4)
 36         {
 37             return Joint(Joint(buffer1, buffer2, buffer3), buffer4);
 38         }
 39 
 40         public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4, byte[] buffer5, byte[] buffer6)
 41         {
 42             return Joint(Joint(buffer1, buffer2, buffer3, buffer4), buffer5, buffer6);
 43         }
 44 
 45         // ....
 46 
 47 
 48         // 将 Stream 转成 byte[] 
 49         public static byte[] StreamToBytes(Stream stream)
 50         {
 51             byte[] bytes = new byte[stream.Length];
 52             stream.Read(bytes, 0, bytes.Length);
 53             // 设置当前流的位置为流的开始
 54             stream.Seek(0, SeekOrigin.Begin);
 55             return bytes;
 56         }
 57 
 58         // 将 byte[] 转成 Stream 
 59         public static Stream BytesToStream(byte[] bytes)
 60         {
 61             Stream stream = new MemoryStream(bytes);
 62             return stream;
 63         }
 64 
 65         // 将 byte[] 转成 string 
 66         public static string BytesToString(byte[] bytes)
 67         {
 68             UnicodeEncoding converter = new UnicodeEncoding();
 69             string s = converter.GetString(bytes);
 70             return s;
 71         }
 72 
 73         // string 转成 byte[] 
 74         public static byte[] StringToBytes(string s)
 75         {
 76             UnicodeEncoding converter = new UnicodeEncoding();
 77             byte[] bytes = converter.GetBytes(s);
 78             return bytes;
 79         }
 80 
 81         //Stream 和 文件之间的转换
 82         //将 Stream 写入文件
 83         public static void StreamToFile(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         //从文件读取 Stream
 98         public static Stream FileToStream(string fileName)
 99         {
100             // 打开文件
101             FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
102             // 读取文件的 byte[]
103             byte[] bytes = new byte[fileStream.Length];
104             fileStream.Read(bytes, 0, bytes.Length);
105             fileStream.Close();
106             // 把 byte[] 转换成 Stream
107             Stream stream = new MemoryStream(bytes);
108             return stream;
109         }
110 
111         //二进制转换成图片
112         public static Image bytesToImage(byte[] bytes)
113         {
114             MemoryStream ms = new MemoryStream(bytes);
115             Image img = Image.FromStream(ms);
116             return img;
117         }
118 
119         //图片 转换成 byte
120         public static byte[] ImageTobytes(Image img)
121         {
122             Bitmap BitReturn = new Bitmap(img);
123             byte[] bytes = null;
124             MemoryStream ms = new MemoryStream();
125             BitReturn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
126             bytes = ms.GetBuffer();
127             return bytes;
128         }
129 
130         // ....
131 
132     }
133 
134 }
原文地址:https://www.cnblogs.com/endv/p/6013933.html