将几个 .jpg 从文件添加到图像数组 [英] Add several .jpg's from file to image array

查看:23
本文介绍了将几个 .jpg 从文件添加到图像数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会保持简短...我之前问过这个问题,但没有人回答.我想从一个文件夹中获取多个图像,并将它们添加到一个数组中.这不起作用,有人可以明确告诉我该怎么做吗?谢谢!

I'll keep this short and simple... I asked this question before, but It wasn't answered. I'd like to get several images from a folder, and add them to an array. This is not working, can someone explicitly tell me how to do it? Thanks!

在表单加载时:

Private Sub Button1_Add(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim PictureArray As New List(Of Image)
    For Each item As String In Directory.GetFiles("C:\Users\turcotd\Desktop\ITLPers", "*.jpg", IO.SearchOption.AllDirectories)
        Dim _Image As Image = Image.FromFile(item)
        PictureArray.Add(_Image)
    Next

    If (i < 6) Then
        Dim pb As New PictureBox
        Me.FlowLayoutPanel1.Controls.Add(pb)
        pb.Image = PictureArray(i)
        i = i + 1

谢谢!!!

推荐答案

首先,我向您展示了如何获取 DirectoryInfo 不是来自上面的 FileInfo!

First, i've showed you how to get the images of a DirectoryInfo not from a FileInfo like above!

我在我对您上一个问题的回答中使用了强类型List(Of FileInfo) 而不是数组,因为它更好,甚至比一个 ArrayList .您可以像访问数组中的项目一样访问列表中的项目(通过索引或foreach").

I've used a strong typed List(Of FileInfo) in my answer on your previous question instead of an Array because it's a lot better, even than an ArrayList. You can access the items in a List the same you would access the items in an Array(via Index or "foreach").

如果您仍然坚持使用数组,则只需使用 ToArray-Extension 而不是 ToList.例如:

If you anyway insist on using an Array, you simply need to use the ToArray-Extension instead of ToList. For example:

Dim imageArray = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToArray

查看 FileInfo-Class 以了解进一步的信息.例如,您需要调用 FileInfo.Name 获取图像名称(不带路径的文件名)或 FileInfo.FullName 获取完整路径.

Look at the FileInfo-Class for further informations. For example you need to call FileInfo.Name to get the image-name(filename without path) or FileInfo.FullName to get the Full-Path.

所以这应该有效:

Dim dir = New IO.DirectoryInfo("C:\Users\turcotd\Desktop\ITLPers")
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToList
Dim pictures As New List(Of PictureBox)
For Each img In images
    Dim picture As New PictureBox
    picture.Image = Image.FromFile(img.FullName)
    pictures.Add(picture)
Next

这篇关于将几个 .jpg 从文件添加到图像数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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