byte[]與string轉換
參考網址:https://www.cnblogs.com/xskblog/p/6179689.html
1、使用System.Text.Encoding.Default,System.Text.Encoding.ASCII,System.Text.Encoding.UTF8等。還可以使用System.Text.Encoding.UTF8.GetString(bytes).TrimEnd('\0')給字符串加上結束標識。(試用感覺還可以,編碼方式需對應上)
還可以指定編碼方式:System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
字符串是亂碼,但數據沒丟失
string str = System.Text.Encoding.UTF8.GetString(bytes); byte[] decBytes = System.Text.Encoding.UTF8.GetBytes(str);
2、沒有使用,不知道效果如何
string str = BitConverter.ToString(bytes); String[] tempArr = str.Split('-'); byte[] decBytes = new byte[tempArr.Length]; for (int i = 0; i < tempArr.Length; i++) { decBytes[i] = Convert.ToByte(tempArr[i], 16); }
3、此方法里可能會出現轉換出來的string可能會包含 '+','/' , '=' 所以如果作為url地址的話,需要進行encode(具體如何沒有嘗試,因為我並不是轉換的網址,感覺用着還可可以,我用來存數據庫了) 出現數據丟失
string str = Convert.ToBase64String(bytes); byte[] decBytes = Convert.FromBase64String(str);
圖片到byte[]再到base64string的轉換(備用)
//在C#中 //圖片到byte[]再到base64string的轉換: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); string pic = Convert.ToBase64String(arr); //base64string到byte[]再到圖片的轉換: byte[] imageBytes = Convert.FromBase64String(pic); //讀入MemoryStream對象 MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length); memoryStream.Write(imageBytes, 0, imageBytes.Length); //轉成圖片 Image image = Image.FromStream(memoryStream); //現在的數據庫開發中:圖片的存放方式一般有CLOB:存放base64string BLOB:存放byte[] // 一般推薦使用byte[]。因為圖片可以直接轉換為byte[]存放到數據庫中若使用base64string 還需要從byte[]轉換成base64string 。更浪費性能。
4、此方法會自動編碼url地址的特殊字符,可以直接當做url地址使用。但需要依賴System.Web庫才能使用。
string str = HttpServerUtility.UrlTokenEncode(bytes); byte[] decBytes = HttpServerUtility.UrlTokenDecode(str);
5、自己寫方法 (出現數據丟失)
public static string ByteToString(byte[] bytes) { StringBuilder strBuilder = new StringBuilder(); foreach (byte bt in bytes) { strBuilder.AppendFormat("{0:X2}", bt); } return strBuilder.ToString(); } public static byte[] StringToByte(string str) { byte[] bytes = new byte[str.Length / 2]; for (int i = 0; i < str.Length / 2; i++) { int btvalue = Convert.ToInt32(str.Substring(i * 2, 2), 16); bytes[i] = (byte)btvalue; } return bytes; }
byte[]與Image轉換
(參考網址:http://www.cnblogs.com/luxiaoxun/p/3378416.html)
1、把一張圖片(png bmp jpeg bmp gif)轉換為byte數組存放到數據庫。
2、把從數據庫讀取的byte數組轉換為Image對象,賦值給相應的控件顯示。
3、從圖片byte數組得到對應圖片的格式,生成一張圖片保存到磁盤上。
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; namespace NetUtilityLib { public static class ImageHelper { /// <summary> /// Convert Image to Byte[] /// </summary> /// <param name="image"></param> /// <returns></returns> public static byte[] ImageToBytes(Image image) { ImageFormat format = image.RawFormat; using (MemoryStream ms = new MemoryStream()) { if (format.Equals(ImageFormat.Jpeg)) { image.Save(ms, ImageFormat.Jpeg); } else if (format.Equals(ImageFormat.Png)) { image.Save(ms, ImageFormat.Png); } else if (format.Equals(ImageFormat.Bmp)) { image.Save(ms, ImageFormat.Bmp); } else if (format.Equals(ImageFormat.Gif)) { image.Save(ms, ImageFormat.Gif); } else if (format.Equals(ImageFormat.Icon)) { image.Save(ms, ImageFormat.Icon); } byte[] buffer = new byte[ms.Length]; //Image.Save()會改變MemoryStream的Position,需要重新Seek到Begin ms.Seek(0, SeekOrigin.Begin); ms.Read(buffer, 0, buffer.Length); return buffer; } } /// <summary> /// Convert Byte[] to Image /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static Image BytesToImage(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); Image image = System.Drawing.Image.FromStream(ms); return image; } /// <summary> /// Convert Byte[] to a picture and Store it in file /// </summary> /// <param name="fileName"></param> /// <param name="buffer"></param> /// <returns></returns> public static string CreateImageFromBytes(string fileName, byte[] buffer) { string file = fileName; Image image = BytesToImage(buffer); ImageFormat format = image.RawFormat; if (format.Equals(ImageFormat.Jpeg)) { file += ".jpeg"; } else if (format.Equals(ImageFormat.Png)) { file += ".png"; } else if (format.Equals(ImageFormat.Bmp)) { file += ".bmp"; } else if (format.Equals(ImageFormat.Gif)) { file += ".gif"; } else if (format.Equals(ImageFormat.Icon)) { file += ".icon"; } System.IO.FileInfo info = new System.IO.FileInfo(file); System.IO.Directory.CreateDirectory(info.Directory.FullName); File.WriteAllBytes(file, buffer); return file; } } }
