天天看點

[轉]C#中圖檔.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 。更浪費性能。