简体   繁体   English

无法显示图像从Sqlite到WPF窗口

[英]Unable to display image from Sqlite to wpf window

i am trying insert image as blob in Sqlite and trying to retrieve the same to display in Table control by creating row dynamically. 我试图在Sqlite中将图像作为Blob插入,并尝试通过动态创建行来检索要在Table控件中显示的图像。

i could able to do all the steps, inserting to the Sqlite, retrieving and then creating image, tablecell and tablerow respectively. 我能够执行所有步骤,分别插入到Sqlite中,检索然后创建图像,表格单元格和表格行。 But somewhere it is going wrong, Image is not getting displayed on the final window. 但是在某些地方出错了,图像没有显示在最终窗口中。

My workouts: 我的锻炼:
Inserting:- 插入: -


    string aQuery = "INSERT INTO images (FileType,OrgFile) VALUES ('JPEG','@image')";
    SQLiteParameter AddParameter = new SQLiteParameter("@image", System.Data.DbType.Binary);
    AddParameter.Value = File.ReadAllBytes("C://sample.jpg");
    SQLiteCommand SqliteCommand = new SQLiteCommand(aQuery, SqliteCon);
    SqliteCommand.Parameters.Add(AddParameter);
    SqliteCommand.ExecuteNonQuery();
    

Retrieving:- 检索: -
\n    BitmapImage BImg = new BitmapImage(); Image Img = new Image(); SQLiteCommand SqliteCommand = new SQLiteCommand("select OrgFile from user_images where UserID=1", SqliteCon); SQLiteDataReader myDataReader = SqliteCommand.ExecuteReader(); while (myDataReader.Read()) ResultBytes = GetBytes(myDataReader); using (MemoryStream ImgMs = new MemoryStream(ResultBytes)) { BImg.BeginInit(); BImg.StreamSource = ImgMs; BImg.StreamSource.Seek(0, SeekOrigin.Begin); BImg.EndInit(); 
Img.Source = BImg; Img.Width = 1000; Img.Height = 1000; }
\n BitmapImage BImg = new BitmapImage(); Image Img = new Image(); SQLiteCommand SqliteCommand = new SQLiteCommand("select OrgFile from user_images where UserID=1", SqliteCon); SQLiteDataReader myDataReader = SqliteCommand.ExecuteReader(); while (myDataReader.Read()) ResultBytes = GetBytes(myDataReader); using (MemoryStream ImgMs = new MemoryStream(ResultBytes)) { BImg.BeginInit(); BImg.StreamSource = ImgMs; BImg.StreamSource.Seek(0, SeekOrigin.Begin); BImg.EndInit();
Img.Source = BImg; Img.Width = 1000; Img.Height = 1000; }

Adding to Table:- 添加到表:
\n    TableRow TableRow = new TableRow(); InlineUIContainer ImgUICont = new InlineUIContainer(Img); Paragraph Paragraph = new Paragraph(ImgUICont); TableCell TableCell = new TableCell(Paragraph); TableCell.Style = (Style)TryFindResource("TableDataCell"); TableRow.Cells.Add(TableCell); UsersTable.Rows.Add(TableRow);\n    TableRow TableRow = new TableRow(); InlineUIContainer ImgUICont = new InlineUIContainer(Img); Paragraph Paragraph = new Paragraph(ImgUICont); TableCell TableCell = new TableCell(Paragraph); TableCell.Style = (Style)TryFindResource("TableDataCell"); TableRow.Cells.Add(TableCell); UsersTable.Rows.Add(TableRow); 
Please help, if anybody could find the issue. 如果有人可以找到问题,请提供帮助。 Thanks. 谢谢。

From MSDN : MSDN

Set the CacheOption to BitmapCacheOption.OnLoad if you wish to close a stream used to create the BitmapImage. 如果您希望关闭用于创建BitmapImage的流,请将CacheOption设置为BitmapCacheOption.OnLoad。 ... ...

So your code should look like this: 因此,您的代码应如下所示:

using (MemoryStream ImgMs = new MemoryStream(ResultBytes))
{
    BImg.BeginInit();
    BImg.CacheOption = BitmapCacheOption.OnLoad;
    BImg.StreamSource = ImgMs;
    BImg.EndInit();
}

change 更改

ResultBytes = GetBytes(myDataReader);

to

ResultBytes = GetBytes(myDataReader[0]);

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

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