C# WPF 显示来自 Mysql 的图像

标签 c# mysql wpf image

我是一名学生,我不擅长编程。 我将每个玩家的图像保存在我的 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/

相关文章:

wpf - WPF DataGrid是否有Expression Dark主题?

wpf 图像源取决于对象属性

c# - 使用 MVVM 更新 WPF 中的派生属性

c# - 如何调试 Visual Studio 2010 中的属性集?

c# - 使用 Azure SignalR 服务和 Azure Functions 进行本地开发

MySql 外键 : ON DELETE NO ACTION behavour - how to leave info in referenced field?

MySQL 连接包含价格、房间和季节的表

mysql - ORDER by rating ASC 在mysql中不起作用

c# - SqlCommand.Parameters.AddWithValue 未返回正确结果

c# - 使用 XamlServices.Save() 序列化 xaml 文档时如何在每个属性后添加换行符