【发布时间】: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