将图像添加到列表视图
我有这个问题。我想将图像添加到listView。确切地说,我想使用 openFileDialog 选择光盘上的图像,将文件加载到应用程序并在 listView 中显示它们。
现在我这样做:
openFileDialog1.Filter = "png (*.png)|*.png";
openFileDialog1.Multiselect = true;
if ( openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
foreach ( var pngFile in files ) {
try {
Bitmap image = new Bitmap( pngFile );
imageList1.Images.Add( image );
} catch {
}
}
listView1.LargeImageList = imageList1;
listView1.Refresh();
}
但是它不起作用。我做错了什么?
编辑
我得到空白列表视图。没有什么错误。
I have this problem. I want to add image to listView. Exactly I want use openFileDialog for choose image on disc, load file to aplication and show them in listView.
Now I do it like this:
openFileDialog1.Filter = "png (*.png)|*.png";
openFileDialog1.Multiselect = true;
if ( openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] files = openFileDialog1.FileNames;
foreach ( var pngFile in files ) {
try {
Bitmap image = new Bitmap( pngFile );
imageList1.Images.Add( image );
} catch {
}
}
listView1.LargeImageList = imageList1;
listView1.Refresh();
}
But it doesn't work. What do I make wrong?
edit
I get blank listView. Nothing error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,没关系。但您只将图像添加到图像列表中。您尚未修改列表视图中实际使用该添加图像的项目。添加此代码行并根据需要进行调整:
同时确保 listView1.LargeImages = imageList1。你在设计器中设置它。
Well, that's fine. But you only added an image to the image list. You haven't modified an item in the list view that actually uses that added image. Add this line of code and tweak as necessary:
Also ensure that listView1.LargeImages = imageList1. You set that in the designer.