This project is read-only.

Images are rendered from database

Mar 8, 2008 at 11:51 PM
Can someone please point me to an example where the images will rendered dynamically from a database as different users browse my site? I probably will have to do it with an ashx handler but was wondering if someone had a live example. By the way I am using VS2008 with LinQ.

I love the control. Thanks a lot for all the hard work and also thanks a lot in advance to anyone who can answer my question.

Thanks,
Amlan
Apr 1, 2008 at 9:29 PM
Hi,

I have been using the same sort of setup as you described where the Gallery structure is pulled from a database as well as the images themselves.

First started by creating the script as usual

<script type="text/javascript">
new SlideShow.Control(new SlideShow.XmlConfigProvider());
</script>

Then the configuration file:

<configuration width="100%" height="400" background="White">
<modules>
<module type="SlideViewer" />
<module type="ProgressBar" />
<module type="SlideDescription" />
<module type="NavigationTray">
<option name="thumbnailViewer.left" value="83" />
</module>
</modules>
<transitions>
<transition type="FadeTransition" name="CrossFadeTransition" />
<transition type="WipeTransition" name="WipeRightTransition">
<option name="direction" value="Right" />
</transition>
</transitions>
<dataProvider type="XmlDataProvider">
<option name="url" value="Data.ashx" />
</dataProvider>
</configuration>

The main point here is that the xml is generated from a data.ashx file which is shown below. This allows dynamic creation of everything.

<%@ WebHandler Language="VB" Class="Data" %>

Imports System
Imports System.Web
Imports System.Data
Imports System.Xml

Public Class Data : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim objGallery As New Devsphere.Gallery ' Object Used to retrieve the Gallery and Image DataSet
Dim objDS As DataSet
Dim objDR As DataRow
Dim objXMLWriter As XmlTextWriter
Dim blnEndAlbum As Boolean = True
Dim strAlbum, strImageURL As String
Dim intCounter As Integer = 1

strImageURL = ""

objDS = New DataSet("GalleryXML")

With objGallery

objDS = .GetGalleryXML

End With

context.Response.Buffer = False

context.Response.Clear()

context.Response.ContentType = "text/xml"

objXMLWriter = New XmlTextWriter(context.Response.Output)

objXMLWriter.Formatting = Formatting.Indented
objXMLWriter.WriteStartDocument()
objXMLWriter.WriteStartElement("data")
objXMLWriter.WriteAttributeString("transition", "CrossFadeTransition")

strAlbum = ""

For Each objDR In objDS.Tables("GalleryXML").Rows

'Use Image ID from Database to create URL to imgHandler.ashx which will actually stream the image

strImageURL = "../imgHandler.ashx?ID=" & objDR("slide_image")

If objDR("album_header") <> strAlbum Then

'Create new XML Album Node if Album has changed

If strAlbum <> "" Then
'Write Closing XML Node to any previous Nodes
objXMLWriter.WriteEndElement()
blnEndAlbum = False
End If

objXMLWriter.WriteStartElement("album")
objXMLWriter.WriteAttributeString("title", objDR("album_header"))
objXMLWriter.WriteAttributeString("description", objDR("album_title"))
objXMLWriter.WriteAttributeString("image", strImageURL)

strAlbum = objDR("album_header")
blnEndAlbum = True

End If


objXMLWriter.WriteStartElement("slide")
objXMLWriter.WriteAttributeString("title", objDR("slide_title"))
objXMLWriter.WriteAttributeString("description", "")
objXMLWriter.WriteAttributeString("image", strImageURL)
objXMLWriter.WriteAttributeString("thumbnail", strImageURL)
objXMLWriter.WriteEndElement()

Next

' Add Tidy </album> Node

If blnEndAlbum Then objXMLWriter.WriteEndElement()

'Write the XML to file and close the myXmlTextWriter
objXMLWriter.WriteEndDocument()

objXMLWriter.Flush()

objXMLWriter.Close()

End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

The data.ashx generates the xml file. I use this method to allow the system to pass authentication details to the webservice that then passes back appropriate dataset

That is more or less it apart from the imgHandler.ashx file which is used to actually generate the image.
<%@ WebHandler Language="VB" Class="imgHandler" %>

Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient

Public Class imgHandler : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim ID As String = context.Request.QueryString("ID")
Dim objSQLDA As SqlDataAdapter
Dim objDS As New DataSet
Dim strSQL As String = ""

If ID <> "" and ID <> "0" then


strSQL = "SELECT imgData, strContentType FROM tblGalleryPics WHERE intImgID = " & ID

objSQLDA = New SqlDataAdapter(strSQL, System.Configuration.ConfigurationManager.ConnectionStrings("CXName").ConnectionString)
objSQLDA.Fill(objDS)

Dim pict As Byte() = objDS.Tables(0).Rows(0)("imgData")
context.Response.ContentType=objDS.Tables(0).Rows(0)("strContentType")
context.Response.OutputStream.Write(pict, 0, pict.Length)

End If

End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class

Hope that this helps you - feel free to get back with any questions.
Apr 19, 2008 at 8:02 PM
I am having issues with the image. I get AGENOT_FOUND 9 times when I load the page. I only have one image in the database so far. I would only expect to get the error for the image and the thumbnail because I have the image hard coded for the album. I use the database to store images for various other parts of the site. I have tested the code used for the silverlight control and the image is in fact being delivered correctly.

I have tried to settging the image path to:
../../EventAlbum/ImgHandler.ashx?ID= (from the root)
ImgHandler.ashx?ID= (it's in the same folder as the page containing the Silverlight control)

I am using a reader instead of a dataset to get the image from the database.
I don't know what to try next.
Apr 28, 2008 at 7:46 PM
There are a few questions to put with regards to this.

First of all - depending upon what version of the .Net Framework you are using you may need to insert a line or two into your web.config file in order to allow it to render ashx files correctly and also check that your IIS has the ashx extension registered correctly. If the slideshow is rendering other images correctly that are static then you need to concentrate on the data.ashx file to ensure that it is correctly building the url to point to the imghandler.ashx file as this is the most common error. As the ashx file isnt found it will return an odd error as it expects an image to stream back.

Please let me know if this doesnt help and examples of code and i shall try to help more
Sep 14, 2009 at 4:06 AM

Hi,

I need some help!

I am new to asp.net and am trying to work with your code sample here. This is a great control and I can get it pulling images from the file system no problem. What I am trying to do is pull images directly from a database on the fly and am working with your code sample. In another little app that I created I am pulling images from the db and displaying them in a grid view control using a .ashx handler. I am not trying this approach to run a slide show.

Some questions regarding some of the code are:

1 - is this a class that you are calling where you are getting your data   Dim objGallery As New Devssphere.Gallery (this is in th edata.ashx code)

2 - is it possible to make available the db tables and columns that you are using so that I can try to understand how all this is wired up.

If you are still available, thanks for your help.

Sep 14, 2009 at 8:58 AM

Hi There,

Please feel free to drop me an email to andrew@devsphere.co.uk and I shall get back to you with any details that you want - I had a look and the app that this was originally written for is still available so I can get all the code details for you.

 

Andy