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 }
相關文章
相關標籤/搜索