Show image from a route

1

I have a web form with an asp: img control in which I want to show an image, of which I only have the complete path of the file (including the file name) that is stored on the server (the path is saved in a table in the database). But I do not know how to do it. Is there an example of how to do it, for example, embedding vb .net code in the aspx page?

EDIT: SOLVED.

I solved it in the following way:

Function to get the image in base64:

Public Function obtenerImagen(rutaOrden As String) As String

    Try

        Dim bytesImagen As Byte() = System.IO.File.ReadAllBytes(rutaOrden)
        Dim imagenBase64 As String = Convert.ToBase64String(bytesImagen)

        Dim tipoContenido As String

        Select Case Path.GetExtension(rutaOrden)

            Case ".jpg"
                tipoContenido = "image/jpg"
            Case ".gif"
                tipoContenido = "image/gif"
            Case ".png"
                tipoContenido = "image/png"
            Case Else
                Return Nothing

        End Select

        Return String.Format("data:{0};base64,{1}", tipoContenido, imagenBase64)

    Catch

        Return Nothing

    End Try

End Function

And from here I call the function to assign the url to the asp image control:

Dim rutaOrden As String = solicitud("adjunto").ToString.Trim
Dim imagenOrden As String = obtenerImagen(rutaOrden)

If imagenOrden = Nothing Then
    imgOrdenMedica.AlternateText = "Error al obtener la imagen de la orden"
    lnkOrden.Visible = False
Else
    imgOrdenMedica.ImageUrl = imagenOrden
End If
    
asked by Willy616 07.06.2017 в 21:49
source

1 answer

1

You can do it in the code-behing of the page:

img.ImageUrl = "Ruta"

Of course, you have to keep in mind that this has to be a relative route.

If in your database you have C:/temp/foto.jpg you will have to transform it in relative, that is, how to get from the source page to where the file is.

    
answered by 08.06.2017 в 16:49