如何在C#中显示从数据库到列表视图的图像? [英] How I can display images from database to listview in C#?
本文介绍了如何在C#中显示从数据库到列表视图的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Open();
listView1.Clear();
ImageList images = new ImageList();
images.ColorDepth = ColorDepth.Depth32Bit;
listView1.LargeImageList = images;
listView1.LargeImageList.ImageSize = new System.Drawing.Size(255, 255);
cmd.Connection = cn;
cmd.CommandText = "select * from table6";
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
var image_buffer = (byte[])(row["picture"]);
MemoryStream image_stream = new MemoryStream(image_buffer);
image_stream.Write(image_buffer, 0, image_buffer.Length);
images.Images.Add(row["picture"].ToString(), new Bitmap(image_stream));
image_stream.Close();
ListViewItem listItem = new ListViewItem();
listItem.Text = row["id"].ToString(); //t
listItem.ImageKey = row["picture"].ToString();
listView1.Items.Add(listItem);
}
我尝试过的事情:
此代码仅显示数据库中的第一张图像,我想显示所有
What I have tried:
this code display just first image in database i want display all
推荐答案
您最大的问题是您选择的图像键.尝试在调试器中添加以下行,并检查key的值:
string key = row["picture"].ToString()
我怀疑,您会发现它始终是"System.Byte[]"
或类似名称.我的猜测是,您将继续覆盖(显示)唯一一张图像,因为您一直使用相同的图像键.
您需要确保每个图像都使用唯一的密钥.我不确定row["id"]
的数据类型.它可能适合您的需求...尝试检查其ToString
值.如果效果不好,则始终可以添加行计数器并使用其值.
也就是说,我担心其他一些问题.我希望您只是在粘贴一些快速测试代码.否则,您会丢失一大堆using
语句(或Dispose
调用).
希望这可以帮助.祝你好运.
-Eric.
Your biggest problem is the image key that you have chosen. Try adding this line, in the debugger, and examining the value of key:
string key = row["picture"].ToString()
I suspect, you''ll find that it is always"System.Byte[]"
or something similar. My guess is that you keep over-writing (and displaying) the one and only image, because you keep using the same image key.
You need to make sure you''re using a unique key for each image. I''m not sure of the data type ofrow["id"]
. It might suit your needs...try examining itsToString
value. If its not good, you can always just add a row counter and use its value.
That said, I worry about some other problems. I''m hoping that you''re simply cut-pasting some quick test code. Otherwise, you are missing a whole bunch ofusing
statements (orDispose
calls).
Hope this helps. Good luck.
-Eric.
在
ItemsPanelTemplate
中使用ListView
和UniformGrid
.我使用以下显示52张扑克牌.当然,您需要先填充收藏集.这是用于WPF应用程序
Use a
ListView
with aUniformGrid
in theItemsPanelTemplate
. I used the following to display 52 playing cards. You need, of course, to fill the collection first. This is for a WPF application
<Grid HorizontalAlignment="Center" >
<ListView ItemsSource="{Binding CardCollection }" >
<ListView.ItemTemplate>
<DataTemplate >
<Grid HorizontalAlignment="Center" >
<Image Source="{Binding CardGraphic}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel >
<ItemsPanelTemplate>
<UniformGrid Columns="13" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
这篇关于如何在C#中显示从数据库到列表视图的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文