2011-09-12 24 views
1

我花了相當多的時間試圖從SQLite中檢索圖像,但是我失敗了。如果有人能幫我解決我在這裏做錯的事情,那將不勝感激。下面是我用來檢索存儲在SQLite中的BLOB圖像的代碼。將圖像從SQLite檢索到C的問題#

SQLiteConnection con = new SQLiteConnection(Properties.Settings.Default.conString); 
con.Open(); 
SQLiteCommand cmd = new SQLiteCommand("SELECT ImageFiles FROM basicconcepts WHERE id=1",con); 
SQLiteDataReader reader=cmd.ExecuteReader(); 
byte[] imageBytes=null; 
while (reader.Read()) 
{ 
    imageBytes = (System.Byte[])reader["ImageFiles"]; 
} 
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); 
ms.Write(imageBytes, 0, imageBytes.Length); 
pictureBox1.Image = Image.FromStream(ms, true); 

以下是錯誤的堆棧跟蹤我得到

System.ArgumentException was caught 
Message=Parameter is not valid. 
Source=System.Drawing 
StackTrace: 
    at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) 
    at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement) 
    at WindowsFormsApplication1.frmMain.getPDF(String c_Name, Int32 pgNo) in C:\Users\Tanmay\Documents\My Projects\E-Excust\E-Excust\E-Excust\frmMain.cs:line 148 
    InnerException: 

PS上述代碼是getPDF方法內。

編輯

我已經找到了我的問題的解決方案。問題是,我非常驚訝地使用了SQL管理工具。我強烈建議不要使用sqlite管理員檢索BLOB圖像。 我開始使用SQLite Expert,一切都開始正常工作。簡直不敢相信我一整天都在rack 012我的頭腦 感謝大家的幫助,特別是那些沒有在這裏回答,但在聊天中幫了我很多的人。非常感謝usernane,yas4891,SomeGuy和其他幾個人

+0

是什麼類型'pictureBox1'?圖像保存時的格式是什麼? –

+0

圖片爲Jpg格式。 pictureBox1因爲我已經在我的表格上添加了一個圖片框 – Tanmay

回答

0

一旦你read the binary data從數據庫byte[]你可以簡單地說:

var ms = new MemoryStream(imageBytes); 
pictureBox1.Image = Image.FromStream(ms, true); 
+0

這是怎麼解決我的問題?它本質上是做同樣的事情嗎?另外我在聊天上檢查它。那裏的人同意我的代碼是正確的 – Tanmay

相關問題