我有一个文件存储在我网站的目录中。当我尝试使用 image.fromfile 访问该文件时,会抛出一个异常,指出该文件不存在。但是,当我使用完全相同的路径访问完全相同的文件但将其加载到图像控件中时,图像会完美加载以验证它是否存在。
抛出文件未找到异常的代码是:
Private Sub btnCombine_Click(sender As Object, e As EventArgs) Handles btnCombine.Click
Dim BMCanvas As Bitmap 'the "canvas" to draw on
Dim BackgroundTemplate As Image 'the background image
Dim img1Overlay As Bitmap 'the character image
BackgroundTemplate = Image.FromFile("~/Account/Images/Blue 1-02.jpg") 'Template Background Image
img1Overlay = Image.FromStream(FileUpload1.FileContent) 'First overlay image
BMCanvas = New Bitmap(500, 647) 'new canvas
Using g As Graphics = Graphics.FromImage(BMCanvas)
g.DrawImage(BackgroundTemplate, 0, 0, 500, 647) 'Fill the convas with the background image
g.DrawImage(img1Overlay, 50, 50, 100, 100) 'Insert the overlay image onto the background image
End Using
'Setup a path to the destination for the composite image
Dim folderPath As String = Server.MapPath("~/OutFiles/")
'Create a directory to store the composite image if it does not already exist.
If Not Directory.Exists(folderPath) Then
'If Directory (Folder) does not exists Create it.
Directory.CreateDirectory(folderPath)
End If
'Temporarily save the file as jpeg.
BMCanvas.Save(folderPath & "Temp1.jpg", Imaging.ImageFormat.Jpeg)
'View the resulting composite image in image control.
Image1.ImageUrl = folderPath & "Temp1.jpg"
BMCanvas.Dispose()
End Sub
验证图像实际上在目录中并成功显示图像的代码是:
Private Sub cboRole_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboRole.SelectedIndexChanged
If cboRole.SelectedIndex = 1 Then
Image1.ImageUrl = "~/Account/Images/Blue 1-02.jpg"
End If
End Sub
我无法弄清楚为什么一种方式有效而另一种方式无效。
我也尝试了以下代码但没有成功:
'Another way to read the image files
Image = File.ReadAllBytes(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "~/Account/Images/Blue 1-02.jpg")))