如何将图像添加到C#Windows窗体的列表视图中?
我正在尝试在.NET framwork(C#)上创建一个windows窗体,其中显示我列表中所有用户的图像列表o(照片库)。我试图编码它,但它没有显示任何图像,只有标签或名称 到目前为止,我在项目中的formload事件中已经有了这组代码如何将图像添加到C#Windows窗体的列表视图中?,c#,asp.net,imagelist,C#,Asp.net,Imagelist,我正在尝试在.NET framwork(C#)上创建一个windows窗体,其中显示我列表中所有用户的图像列表o(照片库)。我试图编码它,但它没有显示任何图像,只有标签或名称 到目前为止,我在项目中的formload事件中已经有了这组代码 private void PicGallery_Load(object sender, EventArgs e) { ShowStudentImages(); } private void ShowStudentImages() { fore
private void PicGallery_Load(object sender, EventArgs e)
{
ShowStudentImages();
}
private void ShowStudentImages()
{
foreach (Students student in _studentList)
{
// Skapa rätt storlek för bilderna - se img control i design
Image thumbLarge = GetThumb(student.PictureStudents, 128);
Image thumbSmall = GetThumb(student.PictureStudents, 32);
// Lägg till bilden i listorna med unik personnummer
imgGalleryLarge.Images.Add(student.PersonNumberStudents, thumbLarge);
imgGallerySmall.Images.Add(student.PersonNumberStudents, thumbSmall);
ListViewItem lvi = new ListViewItem(student.LastnameStudents + " " +
student.FirstnameStudents);
lvi.SubItems.Add(student.PhoneStudents);
lvi.SubItems.Add(student.EmailStudents);
lsvImageGallery.Items.Add(lvi);
}
}
private Bitmap GetThumb(Image image, int maxSize)
{
int tWidth, tHeight, tExtraX, tExtraY;
int width = image.Width;
int height = image.Height;
double whRatio = (double)width / height;
if (image.Width >= image.Height)
{
tWidth = maxSize;
tHeight = (int)(tWidth / whRatio);
}
else
{
tHeight = maxSize;
tWidth = (int)(tHeight * whRatio);
}
tExtraX = (maxSize - tWidth) / 2;
tExtraY = (maxSize - tHeight) / 2;
Bitmap imgthumb = new Bitmap(maxSize, maxSize, PixelFormat.Format24bppRgb);
Graphics imggraphics = Graphics.FromImage(imgthumb);
imggraphics.Clear(Color.White);
imggraphics.InterpolationMode = InterpolationMode.NearestNeighbor;
imggraphics.DrawImage(image, new Rectangle(tExtraX, tExtraY, tWidth, tHeight), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
return imgthumb;
}
试试这个:
int i = 0;
foreach (Students student in _studentList) {
// Skapa rätt storlek för bilderna - se img control i design
Image thumbLarge = GetThumb(student.PictureStudents, 128);
Image thumbSmall = GetThumb(student.PictureStudents, 32);
// Lägg till bilden i listorna med unik personnummer
imgGalleryLarge.Images.Add(student.PersonNumberStudents, thumbLarge);
imgGallerySmall.Images.Add(student.PersonNumberStudents, thumbSmall);
ListViewItem lvi = new ListViewItem(student.LastnameStudents + " " +
student.FirstnameStudents, i++);
lvi.SubItems.Add(student.PhoneStudents);
lvi.SubItems.Add(student.EmailStudents);
lsvImageGallery.Items.Add(lvi);
}
您必须为每个ListViewItem
设置ImageIndex
,上面使用的ListViewItem
的构造函数是:
public ListViewItem(string text, int imageIndex);
\u学生名单
来自哪里?当窗体加载时,它是否有值?我试图显示的图像库是针对项目中的学生列表的。我有一个学生名单的课程,那是学生名单的来源,谢谢你的快速回答。那么,我必须为ListViewItem使用foreach循环?抱歉,有点困惑。@Jonah您可以使用foreach
循环(就像您所做的那样)或普通的for
循环,您必须在这里使用一些循环的原因是您必须在添加到列表视图之前准备每个项目。项目
,您的项目数据由某个集合呈现,因此使用循环是一个明显的方向。谢谢!我会尝试一下,谢谢澄清。我是C语言编程新手,还在学习。我很感激这些提示。