【问题标题】:Problems retrieving Images from SQLite to C#从 SQLite 检索图像到 C# 的问题
【发布时间】:2011-11-15 11:49:18
【问题描述】:

我花了相当多的时间试图从 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);

这是我得到的错误的堆栈跟踪

 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,一切都开始正常了。不敢相信我为此绞尽脑汁一整天 感谢大家的帮助,特别是那些没有在这里回答但在聊天中帮助我很多的人。非常感谢 usernane、yas4891、SomeGuy 和其他一些人

【问题讨论】:

  • pictureBox1 是什么类型?图片保存时是什么格式的?
  • 图片为 Jpg 格式。 pictureBox1 因为我在我的表单上添加了一个图片框

标签: c# sqlite blob


【解决方案1】:

一旦您将数据库中的read the binary data 设为byte[],您就可以:

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

【讨论】:

  • 这将如何解决我的问题?它本质上是在做同样的事情,对吗?我也在聊天中检查了它。那里的人都同意我的代码是正确的
猜你喜欢
  • 1970-01-01
  • 2012-04-28
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2013-11-24
相关资源
最近更新 更多