I would like to understand how to retrieve an Image stored in a mySQL database as a longblob. Once retrieved, assign that image asp.net image control.

Heres the code that uploads the pic to the MYSQL database

If (Not myups.HasFile) Then
            Return -1
        End If
        'FileName.PostedFile.InputStream()

        Try
            Dim fs As FileStream = Nothing

            'Dim img As FileUpload = CType(imgUpload, FileUpload)
            Dim imgByte As Byte() = Nothing
            'If img.HasFile AndAlso Not img.PostedFile Is Nothing Then

            'To create a PostedFile
            Dim File As HttpPostedFile = myups.PostedFile
            'Create byte Array with file len
            imgByte = New Byte(File.ContentLength - 1) {}
            'force the control to load data in array
            File.InputStream.Read(imgByte, 0, File.ContentLength)

When I pull the raw data from the database how do I convert that raw data back to an value that can be assigned to an aspt.net image.

Private Function CreateImage(ByRef pPanel As Panel, ByVal RawData As Byte) As Panel
        Dim myimage As New Image
    myimage.ImageUrl = ?
        pPanel.Controls.Add(myimage)
        Return pPanel
    End Function

Recommended Answers

All 2 Replies

So in the end what I needed to do was to create a handler that could deal with the byte and write it to memory stream. Below is how I grab the LONGBLOB from MYSQL and tie it to an image control to display it.

Handler:

''Call this sub from the page load event of your handler page
Private Sub RenderImage(ByVal ID As String)

        Dim ImgID As Int32
        ImgID = Request.QueryString("PicID")

        Context.Response.ContentType = "image/jpeg"
        Dim strm As IO.MemoryStream = GetMemStream(ID)
        Dim buffer As Byte() = New Byte(4095) {}
        Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)

        Do While byteSeq > 0
            Context.Response.OutputStream.Write(buffer, 0, byteSeq)
            byteSeq = strm.Read(buffer, 0, 4096)
       
        Loop
        Context.Response.BinaryWrite(buffer)
        Return

    End Sub



 Public Function GetMemStream(ByVal Imgno As Integer) As IO.Stream
        Dim conn As String = ""
        Dim connection As New OdbcConnection(conn)
        Dim sql As String = "
        Dim PicData As Byte
        Dim Cmd As New OdbcCommand(sql, connection)

        Cmd.CommandType = CommandType.Text
        Cmd.Parameters.AddWithValue("@ID", empno)
        connection.Open()
        Dim img As Object
        img = Cmd.ExecuteScalar
        'img = Cmd.ExecuteReader

        Try
            Return New IO.MemoryStream(CType(img, Byte()))

        Catch
            Return Nothing
        Finally
            connection.Close()
        End Try
    End Function

Apply the url of the handler in place of the image url. Picture handler is the name of the empty .aspx page I used as a handler.

<img src=""PictureHandler.aspx?Width=500&amp;Height=500&amp;IMGID="  ' + the id of your database image.

This is how I solved my problem I hope this helps people in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.