1

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")))
Jamie
  • 555
  • 3
  • 14

2 Answers2

1

OK, the answer to second part of your question (the "Why it's working with ImageUrl?") is located in documentation

Quote:

Use the ImageUrl property to specify the URL of an image to display in the Image control. You can use a relative or an absolute URL. A relative URL relates the location of the image to the location of the Web page without specifying a complete path on the server. The path is relative to the location of the Web page. This makes it easier to move the entire site to another directory on the server without updating the code. An absolute URL provides the complete path, so moving the site to another directory requires that you update the code.

It (the Image control) already has embedded feature of "Mapping" the path relatively to your web page folder.

When using path from within your code, without controls, like in Image.FromFile, you need to make sure path is correctly mapped, by using Server.MapPath

As you already did in your code for Directory creation.

Dmytro
  • 1,590
  • 14
  • 14
0

You cannot have an Image from a relative url. To get your Image as an System.Drawing.Image you need to get them from a physical path like this (in your case)

 Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(HttpContext.Current.Request.PhysicalApplicationPath & "\Account\Images\Blue 1-02.jpg") 'Server.MapPath("~/Account/Images/Blue 1-02.jpg")) 
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16