如何从 Combobox.selecteditem 显示 Picturebox.image

发布于 2024-12-22 12:34:57 字数 1839 浏览 3 评论 0原文

我正在使用 VB 2008 Express 创建 Windows 窗体应用程序。我有一个名为 cb_face 的组合框。组合框中的项目是使用“foreach”循环从我的资源文件夹填充的图像文件名。当选择一个项目时,我想在 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

I'm using VB 2008 express to create a windows form application. I have a combobox named cb_face. The items in the combobox are image file names populated from my resource folder using a "for each" loop. When an item is selected I would like to display the image in picturebox1. I have tried several different codes but none of them display the image. I am not getting any errors. The comment lines show some of the code that has been tried.

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-12-29 12:34:57

您的位置:

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 目录中。

您需要使用 &附加到目录路径字符串,然后以相同的方式读取文件扩展名。

Where you have:

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)

Change that to something more like:

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")

This assumes based on the rest of your code that the file is a bmp and is located in the Resources directory of the rest of that path.

You need to use & to append to your directory path string and then readd the file extension in the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文