【问题标题】:Image not showing in listview in c#图像未显示在 C# 的列表视图中
【发布时间】:2020-12-21 12:24:47
【问题描述】:

我正在开发 Winforms,我将图像路径保存在数据库中,然后检索路径并将其提供给 Imagelist。 Image 列表中的图像用于 ImageView。但是图像没有显示,它的路径正确显示但图像没有显示。

public void yourvideos(string user, ImageList imageList, ListView lv, Label l,TextBox s)
        {
            cmd = new SqlCommand("select Title,Thumbnail from RecipeInfo where Username=@username", con);
            cmd.Parameters.AddWithValue("@username", user);
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                int count = 0;
                while (reader.Read())
                {
                    s.Text = reader[1].ToString();
                    imageList.ImageSize = new Size(100, 100);
                    imageList.Images.Add("key"+count,Image.FromFile($@"{reader[1]}"));
                   
                    var listviewitem = lv.Items.Add(reader[0].ToString(), count);
                    
                    listviewitem.ImageKey = "key" + count;
                  count++;
                }
                

                
            }
            else
            {

                l.Visible = true;
                l.Text = "Upload videos and share your recipes with others";
            }
            reader.Close();
            con.Close();
        }

【问题讨论】:

  • count++ 我使用了这样每个新图像都有更改图像键
  • 如果查询只返回一个用户,那么您将没有图像(键错误)。如果它返回多个用户,则除第一个(无图像)之外的所有用户的图像都是错误的。只使用lv.Items.Add(reader[0].ToString(), count); 并在之后增加计数器。是时候学习如何调试代码了。
  • 首先计数为 0,然后存储在 imagelist 中的第一个图像的值为 key0,然后计数递增,它的值变为 1,新列表的值为 key1
  • 我通过将 count++ 放在末尾来更正代码
  • 用户是外键,一个用户有多张图片

标签: c# winforms listview imagelist


【解决方案1】:

这个问题在我用key的时候也出现过,我用Index解决了。
你可以参考我的代码:

public Form1()
    {
        InitializeComponent();                
        this.listView1.View = View.LargeIcon;
       yourvideos(this.imageList1,this.listView1);
    }
    public void yourvideos( ImageList imageList1, ListView listView1)
    {
        string sql = "...";
        SqlConnection con = new SqlConnection(sql);
        SqlCommand cmd = new SqlCommand("select Title,Thumbnail from RecipeInfo", con);
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        int i = 0;
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                string title = reader["Title"].ToString();
                string picurl = reader["Thumbnail"].ToString();
                imageList1.ImageSize = new Size(60, 60);
                imageList1.Images.Add(Image.FromFile(picurl));
                ListViewItem item = new ListViewItem();
                item.Text = Convert.ToString(title);
                listView1.Items.Add(item);
                item.ImageIndex = i;
                i++;

            }
        }
        listView1.LargeImageList = imageList1;
        reader.Close();
        con.Close();

    }
       

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多