Home > Net >  Images I dynamically added in a ListView are displaying as a black square, Windows Forms
Images I dynamically added in a ListView are displaying as a black square, Windows Forms

Time:11-21

While populating a ListView via data I have stored in a SQL Server Database, no matter how I tackle this the imageList I'm trying to fill, won't display the images, but instead it displays a black square where the actual image should be.

I have tried all of the imagelist types and the types of Views on the list view but they all fail in the same way.

Here's how I'm trying to populate it:

public ListView List_Of_Albums(ListView LV)
    {
        LV.Clear();
        LV.Columns.Add("Album Name", 233);
        LV.View = View.Details;
        ImageList imgs = new ImageList();
        LV.SmallImageList = imgs;
        LV.BringToFront();
        imgs.ImageSize = new Size(108, 93);
        LV.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        try
        {
            ConexionDB();
            SqlCommand cmd = new SqlCommand("spSelect_Albums", cnx);
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    string[] row =
                    {
                        dr.GetValue(2).ToString(),
                        dr.GetValue(0).ToString(),
                        dr.GetValue(1).ToString(),
                        dr.GetValue(3).ToString(),
                        dr.GetValue(4).ToString(),
                        dr.GetValue(5).ToString(),
                        dr.GetValue(6).ToString(),
                        dr.GetValue(7).ToString(),
                        dr.GetValue(8).ToString(),
                        dr.GetValue(9).ToString()
                    };
                    try
                    {
                        imgs.Images.Add(Image.FromFile(dr.GetValue(4).ToString()));
                    }
                    catch (Exception)
                    {
                        imgs.Images.Add(Properties.Resources.No_Art);
                    }
                    var LVItem = new ListViewItem(row);
                    LV.Items.Add(LVItem);
                }
            }
            cnx.Close();
            return LV;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Database execution error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return LV;
        }
    }

however the result is always this:

enter image description here

I have confirmed that the string used as a path is a valid one, and it does give me a file not found if I put instead a garbage string as the source.

CodePudding user response:

Did ou set the LVI's image index? – TaW

That was it, thanks a lot, if anyone has a similar issue this is how I assigned the image index by using the same while loop used above in my example.

public ListView List_Of_Albums(ListView LV)
    {
        int imgCount = 0; // HERE
        LV.Clear();
        LV.Columns.Add("Album Name", 333);
        LV.View = View.LargeIcon;
        ImageList imgs = new ImageList();
        LV.LargeImageList = imgs;
        LV.BringToFront();
        imgs.ImageSize = new Size(108, 93);
        LV.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        try
        {
            ConexionDB();
            SqlCommand cmd = new SqlCommand("spSelect_Albums", cnx);
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    string[] row =
                    {
                        dr.GetValue(2).ToString(),
                        dr.GetValue(0).ToString(),
                        dr.GetValue(1).ToString(),
                        dr.GetValue(3).ToString(),
                        dr.GetValue(4).ToString(),
                        dr.GetValue(5).ToString(),
                        dr.GetValue(6).ToString(),
                        dr.GetValue(7).ToString(),
                        dr.GetValue(8).ToString(),
                        dr.GetValue(9).ToString()
                    };
                    var LVItem = new ListViewItem(row);
                    LV.Items.Add(LVItem);
                    try
                    {
                        imgs.Images.Add(Image.FromFile(dr.GetValue(4).ToString()));
                        LV.Items[imgCount].ImageIndex = imgCount; //counts up
                    }
                    catch (Exception)
                    {
                        imgs.Images.Add(Properties.Resources.No_Art); // no image if it can't find one
                        LV.Items[imgCount].ImageIndex = -1;
                    }
                    imgCount  ;
                }
            }
            cnx.Close();
            return LV;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), "Database execution error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return LV;
        }
    }
  • Related