我是一名学生,我不擅长编程。 我将每个玩家的图像保存在我的 mysql 数据库中。我创建了一个程序,可以在其中列出数据库中的一些足球运动员。当我单击数据网格中列出的玩家时,会出现一个新窗口,其中包含有关该玩家的信息。一切正常,但现在我希望将数据库中所选玩家的图片显示在信息窗口上。有谁能够帮助我?我的英语不是最好的(我17岁)所以我希望你能理解我的意思。
这是我尝试做的,但我不知道如何继续。附言。它位于 WPF 中。
MySqlCommand cmd = new MySqlCommand("SELECT Bilder FROM spieler WHERE Bilder='{8}'");
MySqlDataReader rdr1 = cmd.ExecuteReader();
try
{
conn.Open();
while (rdr1.Read())
{
// image1... I don't know what to write here
}
}
catch (Exception ex)
{
MessageBox.Show("Fehler: " + ex);
}
rdr1.Close()
最佳答案
只需事先使用byte[]
转换来获取它:
while (rdr1.Read())
{
byte[] data = (byte[])reader[0]; // 0 is okay if you only selecting one column
//And use:
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
Image image = new Bitmap(ms);
}
}
<小时/>
更新: 在 WPF 中,使用 BitmapImage:
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
var imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = ms;
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.EndInit();
// Assign the Source property of your image
yourImage.Source = imageSource;
}
关于C# WPF 显示来自 Mysql 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107489/