2011-01-06 45 views
2

我有一個圖像列在字節DB &位置(其中存儲圖像的路徑), 如果路徑爲空我用圖像列,如果路徑不是空的,我將圖像加載到一個字節數組,並將圖像作爲內存流從文件路徑或直接字節圖像列中返回。圖像到內存流,基於位置

//query 
select image,filepath from tablename; 
byte[] Image = { }; 
if(file path is not empty) 
{ 
System.Net.WebClient Client = new System.Net.WebClient(); 
Image = Client.DownloadData("Path of the image-http://....gif"); 
} 
else 
{ 
Image= datareader.GetOrdinal("image"); 
} 

如何將字節圖像分配給內存流?

回答

3

可以使用MemoryStream構造函數,一個byte[]實例:

byte[] data = // get data... 
using (var stream = new MemoryStream(data)) 
{ 

} 

如果你已經打開的流,然後只寫數組的內容:

stream.Write(data, 0, data.Length); 
+0

謝謝,我是應該以字節的形式從文件位置或圖像(字節列數據類型)中獲取圖像,我有一個方法,當文件路徑發送時返回字節,我現在需要的是根據文件路徑的字節將字節分配給內存流或圖像的字節?任何幫助? – Sharpeye500 2011-01-06 23:44:22

+0

那裏有其他的方法嗎?大師請幫幫我。 – Sharpeye500 2011-01-06 23:50:12

+0

Sharpeye500,請解釋你還需要什麼 - 馬修提供了(只)的方式來創建MemoryStream出陣列。 – 2011-01-07 00:15:27