2013-10-16 34 views
0

我必須從我的系統中獲取圖像並將其以字節的形式保存到服務器中的文件夾中。不過我必須給出.txt文件的路徑,即轉換後的圖像文件通過創建一個表中的數據庫。最後,我想從數據庫中檢索它。它應該是一個Windows應用程序。這可能嗎?保存圖像並從數據庫中檢索

+0

什麼樣的數據庫? – Raptor

+0

這是SQL服務器 –

+0

我們不能給你整個代碼第一顯示你做的..你的代碼..粘貼你的代碼 – Aravind

回答

2

是的,它是可能的...

你只是存儲您創建的映像文件的路徑。 路徑只是一個簡單的字符串。

檢索時,您需要從數據庫中取出路徑,並將其設置爲Windows應用程序中ImageBox的圖像源路徑。

示例:

用於選擇圖像文件。

string DestinationPath = "D:\\test.jpg"; 
OpenFileDialog ofd = new OpenFileDialog(); 
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
{ 
    byte[] bt = File.ReadAllBytes(ofd.FileName); 
    File.WriteAllBytes(DestinationPath, bt); 
} 
//Store DestinationPath into the database. 

檢索和在PictureBox

string pathFromDatabase = "D:\\test.jpg"; //Retrieve from database 
pboxDisplay.Image = Image.FromFile(pathFromDatabase); //Assuming pboxDisplay as the PictureBox control 

希望它有助於顯示...

+0

@病毒。你能分享這個代碼嗎? –

+0

@Jameskumar,用於保存或檢索? – Virus

+0

保存和檢索完整的代碼,我已經在上面解釋過了。 –

0

從數據的基礎上嘗試這種

byte bt = File.ReadAllBytes("C:\\test.jpg"); 
File.WriteAllBytes("C:\\test1.jpg",bt) 

    " bt " you can upload this byte to Database while retrieving bytes from data base use File.WriteAllbytes ... 
+0

這些字節應該保存在不在c盤的服務器上。 –

+0

字節不會保存在C盤...先閱讀文檔... http://msdn.microsoft.com/en-us/library/system.io.file.aspx – Aravind

+0

我們不能給你整個代碼第一顯示笏你做到了 – Aravind