在 vb.net 中显示文件夹/文件中的图像 [英] Displaying image from folder/file in vb.net

查看:50
本文介绍了在 vb.net 中显示文件夹/文件中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim ImagePath As String = "images/spaceship2.png"
Dim img1 As Bitmap
Dim newImage As Image = Image.FromFile("images/spaceship2.png")

img1 = New Bitmap(ImagePath)
pb2.ImageLocation = ImagePath

pb1.Image = newImage

我想显示文件夹中的图片,例如id号为22137471的student,名字为22137471的图片会显示在我的图片框上,中间,我在google的某个地方看到了这个代码.

I want to display image from folder, for example, student with an id number of 22137471, the picture with the name of 22137471 will be display on my picture box, between, i saw this code somewhere in google.

推荐答案

我想显示文件夹中的图像,例如带有 id 的学生编号为 22137471,名称为 22137471 的图片将是显示在我的图片框上

i want to display image from folder, for example, student with an id number of 22137471, the picture with the name of 22137471 will be display on my picture box

尝试类似...

Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
PictureBox1.Image = Image.FromFile(filename)

这是一个不锁定原始图像文件的更新版本:

Here's an updated version that doesn't lock the original image file:

Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
Try
    Using fs As New System.IO.FileStream(filename, IO.FileMode.Open)
        PictureBox1.Image = New Bitmap(Image.FromStream(fs))
    End Using
Catch ex As Exception
    Dim msg As String = "Filename: " & filename &
        Environment.NewLine & Environment.NewLine &
        "Exception: " & ex.ToString
    MessageBox.Show(msg, "Error Opening Image File")
End Try

这篇关于在 vb.net 中显示文件夹/文件中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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