1.將Image圖像文件存入到數據庫中web
咱們知道數據庫裏的Image類型的數據是"二進制數據",所以必須將圖像文件轉換成字節數組才能存入數據庫中.數據庫
//將本地圖片轉換成二進制保存起來 private byte[] SetImageToByteArray(string fileName) { FileStream fs = null; try { fs = new FileStream(fileName, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite); Bitmap bt = new Bitmap(fs); int streamLength = (int)fs.Length; byte[] image = new byte[streamLength]; fs.Read(image, 0, streamLength); return image; } catch (Exception) { throw; } finally { fs.Close(); } }
2.從SQL Server數據庫讀取Image類型的數據,並轉換成bytes[]或Image圖像文件數組
這種方式保存圖片比較安全安全
大體的步驟,就是咱們首先要得到文件的路徑,不管是經過上傳控件和文本框,仍是手動輸入等,只要能得到路徑就能夠了asp.net
而後咱們就能夠利用上面的方法,將文件資源轉換爲二進制放到數據庫裏,數據庫字段能夠使用image類型,或者字符串,到時候轉換下就能夠了ide
最後咱們就能夠從數據庫中讀取字節,轉換成image,或者以流的方式輸出圖片均可以了(這種通常用於asp.net輸入圖片)spa
asp.net輸入流的代碼以下:.net
若是是winForm就能夠直接經過轉換過的image賦值就能夠了3d