有没有更好的方法来选择 VB.NET 中的图像?



我像这样将图片导入到我的 oop 中。但是,目前每当我复制或移动文件时,它显然无法读取它。

frmLayout.picwater.Image = Image.FromFile("C:UsersCeejayDesktopwinBTECGradeAndUcasCalwinBTECGradeAndUcasCalPictureswater2.png")

使用流加载图像

Public Function LoadImageFromStream(ByVal cImage As String) As Image
Dim image As Image = Nothing
Using fs As New System.IO.FileStream(cImage, IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(fs.Length) As Byte
fs.Read(buffer, 0, fs.Length - 1)
Using ms As New System.IO.MemoryStream
ms.Write(buffer, 0, buffer.Length - 1)
image = image.FromStream(ms)
End Using
End Using
Return image
End Function

在你的代码中,你可以像这样调用它

frmLayout.picwater.Image = LoadImageFromStream("C:UsersCeejayDesktopwinBTECGradeAndUcasCalwinBTECGradeAndUcasCalPictureswater2.png")

但是您真的应该考虑@Hans Passant 关于不使用硬编码文件路径的评论

最新更新