c# - WPF - 使用 C# 从 WPF 中的 Mysql 数据库中检索(选择或下载)图像
问题描述
我可以通过这段代码在 C# 中的 WPF 中从 Mysql 数据库下载(检索)图像。我从这个https://www.experts-exchange.com/questions/25096053/Retrieve-images-in-C-WPF-Application-from-SQL-Server-Database.html 网站复制了这段代码。但我不知道这段代码是如何逐行工作的。如果有人知道这方面的知识,请提供帮助。
代码在这里。
string query = "SELECT image_data from image_table WHERE image_id=22";
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
Byte[] bindata = (Byte[])dataReader["image_data"];
MemoryStream strm = new MemoryStream();
strm.Write(bindata, 0, bindata.Length);
strm.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(strm);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
download.Source = bi;
}
解决方案
我可以使用此代码在 c# 中的 wpf 中从数据库中检索图像。这段代码的唯一问题是它一次只能检索一个图像。在使用此代码之前添加 System.Drawing.Imaging。代码中的库。
BitmapImage bi = new BitmapImage();
System.Drawing.Image img;
MemoryStream strm = new MemoryStream();
strm.Write(bindata, 0, bindata.Length);
strm.Position = 0;
img = System.Drawing.Image.FromStream(strm);
bi.BeginInit();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
推荐阅读
- python - 为什么我无法从本网站的超链接中抓取 URL?
- python - 我如何使用在 python 中生成的 kafka 数据
- java - 从位桶管道访问 Salesforce 沙箱
- python - Python3.8赋值表达式,用于列表推导或作为表达式
- cron - 这个错误 >/dev/null 2>/dev/null) 是什么意思
- highcharts - Highcharts shankey图百分比未显示
- html - 视频自动播放在某些 Safari 浏览器上不起作用
- php - 如何根据键名条件从集合中获取价值?
- c++ - MDI选项卡名称变回.../任务栏预览未选择
- python - 保持 robotsframework 关键字库中的应用程序实例处于活动状态