简体   繁体   English

从SQLite到C#检索图像时出现问题

[英]Problems retrieving Images from SQLite to C#

I have spent considerable time trying to retrieve images from SQLite but i have failed miserably. 我花了很多时间尝试从SQLite检索图像,但我失败了。 If some one could help me with what i am doing wrong here, that would be greatly appreciated. 如果有人可以帮助我解决我在这里做错的事情,将不胜感激。 Below is the code i am using to retrieve a BLOB image stored in SQLite. 下面是我用来检索存储在SQLite中的BLOB图像的代码。

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);

Here is the Stack Trace of the error that i am getting 这是我得到的错误的堆栈跟踪

 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 the above code is inside the getPDF method. PS上面的代码在getPDF方法中。

EDIT 编辑

I have found a solution for my problem. 我找到了解决我问题的方法。 The problem was, quite surprisingly the sql admin tool i was using. 问题是,非常令人惊讶的是我正在使用的sql管理工具。 i would strongly recommend not to use sqlite administrator for retrieving BLOB images. 我强烈建议不要使用sqlite管理员来检索BLOB图像。 I started using SQLite Expert and everything started working fine. 我开始使用SQLite Expert,一切正常。 Could not believe i racked my head over this for one whole day Thanks everyone for their help, specially the guys who havent answered here but helped me a lot on chat. 真不敢相信我整整一整天都在努力工作。谢谢大家的帮助,特别是那些在这里没有回答但在聊天中给我很大帮助的家伙。 big thanks to usernane, yas4891, SomeGuy and a few others 非常感谢usernane,yas4891,SomeGuy和其他一些人

Once you read the binary data from your database as byte[] you could simply: 一旦从数据库读取了作为byte[] 的二进制数据 ,您就可以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM