圖片轉(zhuǎn)化為二進(jìn)制數(shù)據(jù),并顯示出來(lái)2009-12-04 17:32:29| 分類: 默認(rèn)分類 | 標(biāo)簽:圖片轉(zhuǎn)化為二進(jìn)制數(shù)據(jù) 字號(hào):大中小 訂閱 1.將Image圖像文件存入到數(shù)據(jù)庫(kù)中
我們知道數(shù)據(jù)庫(kù)里的Image類型的數(shù)據(jù)是"二進(jìn)制數(shù)據(jù)",因此必須將圖像文件轉(zhuǎn)換成字節(jié)數(shù)組才能存入數(shù)據(jù)庫(kù)中. 要這里有關(guān)數(shù)據(jù)的操作略寫,我將一些代碼段寫成方法,方便直接調(diào)用. //根據(jù)文件名(完全路徑) public byte[] SetImageToByteArray(string fileName) { FileStream fs = new FileStream(fileName, FileMode.Open); int streamLength = (int)fs.Length; byte[] image = new byte[streamLength]; fs.Read(image, 0, streamLength); fs.Close(); return image; } //另外,在ASP.NET中通過FileUpload控件得到的圖像文件可以通過以下方法 public byte[] SetImageToByteArray(FileUpload FileUpload1) { Stream stream = FileUpload1.PostedFile.InputStream; byte[] photo = new byte[FileUpload1.PostedFile.ContentLength]; stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength); stream.Close(); return photo; } 2.從SQL Server數(shù)據(jù)庫(kù)讀取Image類型的數(shù)據(jù),并轉(zhuǎn)換成bytes[]或Image圖像文件 //要使用SqlDataReader要加載using System.Da //將數(shù)據(jù)庫(kù)中的Image類型轉(zhuǎn)換成byte[] public byte[] SetImage(SqlDataReader reader) { return (byte[])reader["Image"];//Image為數(shù)據(jù)庫(kù)中存放Image類型字段 } //將byte[]轉(zhuǎn)換成Image圖像類型 //加載以下命名空間using System.Drawing;/using System.IO; using System.Da public Image SetByteToImage(byte[] mybyte) { Image image; MemoryStream mymemorystream = new MemoryStream(mybyte,0, mybyte.Length); image = Image.FromStream(mymemorystream); return image; } |
|
來(lái)自: orion360doc > 《Oracle存儲(chǔ)圖片》