I have a file stored in a directory within my site. When I attempt to access the file using image.fromfile, an exception is thrown saying that the file is not there. However, when I access the exact same file using the exact same path but loading it into an image control, the image loads flawlessly verifying that it is there.
The code that throws the file not found exception is:
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
And the code that verifies that the image is in-fact in the directory and successfully displays the image is:
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
I cannot figure out why one way works and the other way does not.
I have also tried the following code without success:
'Another way to read the image files
Image = File.ReadAllBytes(Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "~/Account/Images/Blue 1-02.jpg")))