NO IMAGE

最近專案中遇到需要json傳輸上百個圖示

進行了多種二進位制轉String的嘗試,最終轉得的String完全不對

自己估計是編碼格式問題,查詢資源發現basic64專做二進位制陣列和String之間的裝換,

將Byte與字元相互裝換

以下是C#實現的 json可用的 圖片轉String  String轉圖片 的程式碼

/// <summary>Convert bitmap to Base64 String
/// </summary>
/// <param name=”bitmap”></param>
/// <returns></returns>
public static String Jpeg2String(Bitmap bitmap)
{
using (Stream stream = new System.IO.MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Seek(0, SeekOrigin.Begin);
Byte[] picData = new byte[stream.Length];
stream.Read(picData, 0, picData.Length);
String sbmp = System.Convert.ToBase64String(picData);
return sbmp;
}
}

/// <summary> Convert base64 String to Jpeg
/// </summary>
/// <param name=”base64Str”></param>
/// <returns></returns>
public static Bitmap GetBitMap(String base64Str)
{
if (!String.IsNullOrEmpty(base64Str))
{
using (Stream stream = new MemoryStream())
{
byte[] picData = System.Convert.FromBase64String(base64Str);
stream.Write(picData, 0, picData.Length);
stream.Seek(0, SeekOrigin.Begin);
Bitmap bitmap = new Bitmap(stream);
return bitmap;
}
}
return null;
}