2012-11-17 49 views
0

我有这个ListView,我想将图像加载到该ListView。我有存储图像的文件路径。我想为包含在文件路径中的每个图像循环并将它们加载到列表视图中。我不知道该怎么做。你能帮我吗?以下是我未完成的代码。谢谢。如何将图像加载到列表视图

DataTable dtPath = new DataTable(); 
dtPath = ContrPtMRD.SelectFilePaths(); 

foreach (DataRow rows in dtPath.Rows) 
{ 
    lvPtMedicalRecord.???? 
} 
+0

我使用列表视图。 – Trax

+0

看看这个:这是[msdn论坛帖子](http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/876b6517-7306-44b0-88df-caebf3b1c10f),它提供了帮助你可能需要。 :) – bonCodigo

+0

@Mr_Green如何让listview中的图像显得更大?我尝试更改'lvPtMedicalRecord.LargeImageList = myImageList;'但图像仍然很小。 – Trax

回答

3

看来你正在将Image locations存储在同一个DataTable中。如果您在处理foreach循环时将这些图像位置存储在ImageList中,这将是可以理解的。下面是示例代码:

lvPtMedicalRecord.LargeImageList = myImageList; //Attaching ImageList to the ListView 
int imageIndex = 0; 
foreach (DataRow rows in dtPath.Rows) 
{ 
    //Store the paths of the images in the same DataTable (I can think of this only) 
    myImageList.Images.Add(Image.FromFile(row[0].ToString()); 
    ListViewItem lvi = new ListViewItem(); 
    lvi.ImageIndex = imageIndex; //Index of the Image present in the `ImageList` 
    imageIndex++; 
    lvPtMedicalRecord.Items.Add(lvi); 
} 

更新:

为了使图像大:

myImageList.ImageSize = new System.Drawing.Size(112, 112); // width, height