从随机图片框获取文件名

时间:2019-06-23 22:29:31

标签: vb.net

我有一个图片框,它随机显示目录中的图片,我想从图片框到标签中获取每张图片的名称

 Dim rnd As New Random
    Dim alljpgs() As String = IO.Directory.GetFiles("C:\Users\" + Environment.UserName.ToString + "\AppData\SystemInfo\Images", "*.jpg", IO.SearchOption.TopDirectoryOnly)
    Dim jpg As New Bitmap(alljpgs(rnd.Next(0, alljpgs.Count - 1)))
    PictureBox14.Image = jpg

    ' Label24.Text = (IO.Path.GetFileNameWithoutExtension(jpg.ToString))
    Dim x As String = Path.GetFileName(PictureBox14.ImageLocation)
    Label24.Text = x

但是如何执行此操作不起作用?

1 个答案:

答案 0 :(得分:0)

将随机选择的图像的索引存储在变量i中。然后使用它来设置PictureBoxLabel的内容。

Dim rnd As New Random
Dim alljpgs() As String = IO.Directory.GetFiles("C:\Users\" + Environment.UserName.ToString + "\AppData\SystemInfo\Images", "*.jpg", IO.SearchOption.TopDirectoryOnly)
Dim i as Integer = rnd.Next(0, alljpgs.Count - 1)
Dim jpg As New Bitmap(alljpgs(i))
PictureBox14.Image = jpg
Label24.Text = IO.Path.GetFileNameWithoutExtension(alljpgs(i))