我正在使用VB 2008 express来创建一个Windows窗体应用程序。我有一个名为cb_face的组合框。组合框中的项目是使用“for each”循环从我的资源文件夹填充的图像文件名。选择项目时,我想在picturebox1中显示图像。我尝试了几种不同的代码,但没有一种显示图像。我没有收到任何错误。注释行显示了一些已经尝试过的代码。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ImgFolder As New IO.DirectoryInfo("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources")
Dim ImgFile As IO.FileInfo() = ImgFolder.GetFiles("*.bmp")
Dim info As IO.FileInfo
For Each info In ImgFile
Dim FaceName As String = IO.Path.GetFileNameWithoutExtension(info.FullName)
CB_Face.Items.Add(FaceName)
Next
End Sub
Private Sub CB_Face_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_Face.SelectedIndexChanged ', CB_Type.SelectedIndexChanged
If CB_Face.SelectedValue IsNot Nothing Then
'Load the image from the full file path
'PictureBox1.ImageLocation = CStr(CB_Face.SelectedItem.ToString)
'PictureBox1.Image = CB_Face.Items(CB_Face.SelectedItem).ItemData
'Dim pic = CType(My.Resources.ResourceManager.GetObject(CStr(CB_Face.SelectedItem)), Image)
'PictureBox1.Image = pic
'PictureBox1.Image = CB_Face.SelectedItem
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources"(CB_Face.SelectedItem.ToString).ToString)
'PictureBox1.ImageLocation = CB_Face.SelectedItem(Name)
'Try
'PictureBox1.Image = Image.FromFile(CB_Face.SelectedItem.ToString)
'Catch ex As Exception
'End Try
'PictureBox1.Image = DirectCast(CB_Face.SelectedItem, Image)
'CType(CB_Face.SelectedItem, Image)
End If
End Sub
答案 0 :(得分:1)
你在哪里:
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources"(CB_Face.SelectedItem.ToString).ToString)
将其改为更像:
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources\" & (CB_Face.SelectedItem.ToString) & ".bmp")
这假设基于代码的其余部分,该文件是bmp并且位于该路径其余部分的Resources目录中。
你需要使用&附加到目录路径字符串,然后以相同的方式读取文件扩展名。