Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Retrieving stored image into aspx page (VB / aspx)


Recommended Posts

Posted

I've managed to get an image into a database now I'm just struggling a little to actually get it back out onto a aspx webpage.

 

Tried a few things and don't seem to making good progress , I've got the image into a dataset but I'm unable to transform that back into a jpg.

 

Anyone else done this ?

 

I've got as far as this :-

 

Dim ds As New DataSet

       Dim sql As String = "Select additionaldiagram from bluediagram where ID = 24"

       Dim conn As New System.Data.SqlClient.SqlConnection(access.SQLstrconn)
       Dim da As New System.Data.SqlClient.SqlDataAdapter(sql, conn)


       da.Fill(ds)
       conn.Close()
       da.Dispose()

       If ds.Tables(0).Rows(0)(0) Is System.DBNull.Value Then
           Image1.Visible = False

       Else

           Dim bytes As [byte]() = ds.Tables(0).Rows(0)(0)
           Dim ms As New MemoryStream(bytes)
           Image1 = System.Drawing.Image.FromStream(ms)




       End If

 

Cheers

Posted

Solved this.

 

Created a .ashx file and referenced it e.g.

 

ImageHander.ashx

 

Imports System.Web
Imports System.Data.SqlClient
Public Class ImageHandler : Implements IHttpHandler
   Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
       Dim imageid As String = context.Request.QueryString("ImID")
       Dim conn As New SqlConnection(access.SQLstrconn) 'this is pointing to a class I use, just use your own connection string
       conn.Open()
       Dim cmd As New SqlCommand(Convert.ToString("Select additionaldiagram from bluediagram where ID=") & imageid, conn)
       Dim dr As SqlDataReader = cmd.ExecuteReader()
       dr.Read()
       context.Response.BinaryWrite(DirectCast(dr(0), Byte()))
       conn.Close()
       context.Response.[End]()
   End Sub
   Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
       Get
           Return False
       End Get
   End Property
End Class

 

and in the VB

 

Image1.ImageUrl = "ImageHandler.ashx?ImID=" & (Request.QueryString("id"))

 

Got the job done anyway

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...