图片未显示在C#的ListView中 [英] Image not showing in listview in c#

查看:63
本文介绍了图片未显示在C#的ListView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Winforms上进行工作,在其中将图像的路径保存到数据库中,然后检索该路径并将其提供给Imagelist.图像列表中的图像在ImageView中使用.但是图像未显示,其路径已正确显示,但图像未显示.

I am working on Winforms where I am saving path of image in database, and then retrieving the path and giving it to Imagelist. The images from Image list are used in ImageView. But the image is not displaying, its path is correctly displayed but image is not showing.

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();
        }

推荐答案

当我使用 key 并使用 Index 解决了此问题时,也会发生此问题.br/>您可以参考我的代码:

This problem also occurred when I used the key, and I solved it with the Index.
You can refer to my code:

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();

    }
       

结果:

这篇关于图片未显示在C#的ListView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆