我需要从图像列表中检索图像到图片框。 [英] I need to retrieve an image to a picture box from an image list.

查看:107
本文介绍了我需要从图像列表中检索图像到图片框。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从图像列表中将图像检索到图片框。

我正在使用c#代码然后执行此错误:

I need to retrieve an image to a picture box from an image list.
I am using c# code and then executed this error comes:

'System.Collections.Generic.List<string>' does not contain a definition for 'Images' and no extension method 'Images' accepting a first argument of type 'System.Collections.Generic.List<string>' could be found (are you missing a using directive or an assembly reference?





这里是我的代码



我尝试过:





here is my code

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {

            List<string> imglist = new List<string>();

              imglist=GetImagesPath("C:/outputDirectory");

               for (int x = 0; x < imglist.Count; ++x)
                {
                    Image temp = imglist.Images[x];
                    pictureBox1.Image = temp;
                }
           
        } 


public List<string> GetImagesPath(String folderName)
        {

            DirectoryInfo Folder;
            FileInfo[] Images;

            Folder = new DirectoryInfo(folderName);
            Images = Folder.GetFiles();
            List<string> imagesList = new List<string>();

            for (int i = 0; i < Images.Length; i++)
            {
                imagesList.Add(String.Format(@"{0}/{1}", folderName, Images[i].Name));
               
            }

            double a = imagesList.Count();
            textBox1.Text = a.ToString();


            return imagesList;
            
        }

推荐答案

这里有几件事:

首先,不要使用名称图像列表 - 有一个名称的控件 ImageList类(System.Windows.Forms) [ ^ ]可用于向PictureBox提供图像,因此如果您不恰当地使用该名称,您会感到困惑。

其次,不要使用string.Format一起加入路径 - 使用Path.Combine,因为它会智能地对spearators进行排序。但是,在这种情况下,使用FileInfo.FullName属性要简单得多:

There are a couple of things here:
Firstly, don't use the name "Image List" - there is a control of that name ImageList Class (System.Windows.Forms)[^] which can be used to provide images to a PictureBox, so you will confuse people if you use the name inapropriately.
Second, don't use string.Format to join a path together - use Path.Combine as it sorts out spearators intelligently. In this case however, it's much simpler to use the FileInfo.FullName property instead:
for (int i = 0; i < Images.Length; i++)
{
    imagesList.Add(Images[i].FullName);
}



但是你需要小心 - 特别是对于图像,Windows会将文件添加到文件夹以进行索引和缩略图,所以你找到的所有文件都是DirectoryInfo.GetFiles不是图像!

如果您使用此代码:


But you need to be careful - particularly with images, Windows will add files to the folder for indexing and thumbnails, so all the files you find with DirectoryInfo.GetFiles will not be images!
If you use this code:

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
List<string> imageExtensions = new List<string>();
foreach (ImageCodecInfo codec in codecs)
    {
    string[] parts = codec.FilenameExtension.Split(';');
    foreach (string extension in parts)
        {
        imageExtensions.Add(extension);
        }
    }



如果您将列出系统中已知的所有图像文件扩展名,并且您可以使用目录。 GetFiles在循环中使用搜索字符串来获取路径中的所有图像(以及图像):


If will give you a list of all the image file extensions that are known on your system, and you can use Directory.GetFiles with a search string in a loop to fetch all the images (and just the images) in the path:

List<string> files = new List<string>();
foreach (string ext in imageExtensions)
    {
    string[] paths = Directory.GetFiles(@"D:\Temp\", ext);
    files.AddRange(paths);
    }



但这会获取图像的路径 - 您无法直接将其加载到PictureBox中。相反,您必须从磁盘加载图像:


But that fetches the path to the image - you can't load that into a PictureBox directly. Instead, you have to load the image from the disk:

myPictureBox.Image = Image.FromFile(files[0]);



并且不要在你尝试的循环中这样做,否则你将在那里加载每个图像并覆盖它们 - 所以你结束只有最后一个!


And don't do that in a loop as you are trying or you will load each and every image in there and overwrite them all - so you end up with just the last one!


请参阅我对该问题的评论。我不知道你可能需要什么样的帮助,除了环顾四周和逻辑思考的建议。



也许这个链接可以给你一些想法: ImageList类(System.Windows.Forms) [ ^ ]。



-SA
Please see my comment to the question. I have no idea what kind of help you may need, except the advice to look around and think logically.

Perhaps this link can get you some ideas: ImageList Class (System.Windows.Forms)[^].

—SA


这篇关于我需要从图像列表中检索图像到图片框。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆