Good afternoon,
I am trying to load images into an ImageViewer control from a database table that has the images stored in a binary column. After determining I can't just bind the imageviewer directly (right?), I wrote some code to pull each image in, write them out to a temp file, and then add each of those items into the imageviewer.
When I did a quick test of adding an image into the imageviewer during the page load, it worked fine. However in this case I will not be loading the images until after a certain record is selected. I am getting no errors, but I'm thinking perhaps the images are not showing up due to this happening sans post perhaps? I placed the imageviewer control inside an UpdatePanel and called Update() after adding the images, as was suggested in another post, but this didn't fix anything. Any ideas?
currentImage = 1 Dim docID As Integer Dim docDB As String Dim myRow As DataRow For Each myRow In docIDs.Tables(0).Rows() docID = myRow.Item(0) docDB = myRow.Item(4)
Dim ConnectionString As String = "Data Source=XXXXXXX;Persist Security Info=True;Initial Catalog=" & docDB & ";User ID=XXXXXXX;Password=XXXXXXX" Dim cn As New SqlClient.SqlConnection(ConnectionString) Dim cmd As SqlClient.SqlCommand Dim dbImage As Byte() = Nothing
cmd = New SqlClient.SqlCommand("SELECT data FROM documents_storage WHERE document_id = " & docID, cn) cmd.Connection.Open() Try dbImage = cmd.ExecuteScalar() If (Not (dbImage Is Nothing)) Then Dim stmBLOBData As New System.IO.MemoryStream(dbImage)
cmd.Connection.Close() 'Take this and save to file. Dim outStream As System.IO.FileStream = System.IO.File.OpenWrite(My.Computer.FileSystem.CurrentDirectory & "/images/" & currentImage & ".jpg") stmBLOBData.WriteTo(outStream) outStream.Flush() outStream.Close()
'Return (stmBLOBData) End If Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical) End Try currentImage = currentImage + 1
Next
Dim imgs() As String = System.IO.Directory.GetFiles(My.Computer.FileSystem.CurrentDirectory & "\images", "*.jpg") For Each img As String In imgs imageViewer.Items.Add(New Infragistics.Web.UI.ListControls.ImageItem(My.Computer.FileSystem.CurrentDirectory & System.IO.Path.GetFileName(img), "", "")) Next
imageViewer.SelectedIndex = 0 UpdatePanel.Update()
Note that most of this is just test code so not really prettied up yet, and thus the images are simply named 1.jpg, 2.jpg, etc.
Hello Craig,
You do not need to make any temporary file. You just need to make a custom HttpHandler, which will handle your images. I have attached a very simple sample web site that has a custom HttpHandler which reads binary data from image file, but you could make it very easy read binary data from your database.
Please review the file Image.ashx, which will show you how to handle binary data and put it on the web.
Now, having Image.ashx, the URL for images becomes something like:localhost/.../Image.ashx
And having your database, you can easily create such dynamic URLs for your images.
I have also added a sample with a custom type - customImage class. Then I bound WebImageViewer to a generic List<customImage> so I illustrated how to make a dadabinding with dynamic images.
I hope this example will help you solving your issues.
For the exmples I used NetAdvantage for Web Client 2009 Vol. 1, which you can freely download for evaluation purposes.
Thanks for your reponses... I have altered my program to include this line of thought at this is what I ended up with in the Image.ashx sub ProcessRequest (note I converted it to VB.NET for my purposes):
context.Response.ContentType = "image/jpg" If Not String.IsNullOrEmpty(context.Request("id")) Then Dim id As Integer = Integer.Parse(context.Request("id")) Dim database As String = context.Request("database").ToString() Dim ConnectionString As String = "Data Source=XXXXXXX;Persist Security Info=True;Initial Catalog=" & database & ";User ID=XXXXXX;Password=XXXXX" Dim cn As New SqlClient.SqlConnection(ConnectionString) Dim cmd As SqlClient.SqlCommand Dim dbImage As Byte() = Nothing Dim br As BinaryReader
cmd = New SqlClient.SqlCommand("SELECT data FROM documents_storage WHERE document_id = " & id, cn) cmd.Connection.Open() Try dbImage = cmd.ExecuteScalar() If (Not (dbImage Is Nothing)) Then Dim stmBLOBData As New System.IO.MemoryStream(dbImage) cmd.Connection.Close() MsgBox("WORK!") br = New BinaryReader(stmBLOBData) context.Response.OutputStream.Write(br.ReadBytes(br.BaseStream.Length), 0, br.BaseStream.Length) End If Catch ex As Exception MsgBox(ex.ToString, MsgBoxStyle.Critical) End Try End If
This seems like it should work to me, based on how you describe it. I know it is passing the correct values into the Image.ashx handler as I put a bit of code in the GetList function to spit out to me what was set. No errors are being thrown, I'm just not getting anything on the imageviewer
imageViewer.DataSource = ImageList.GetList(docIDs) imageViewer.DataBind()
Public Shared Function GetList(ByVal docIDs As DataSet) As ImageList Dim il As New ImageList Dim ci As New CustomImage
Dim docID As Integer Dim docDB As String Dim myRow As DataRow For Each myRow In docIDs.Tables(0).Rows() docID = myRow.Item(0) docDB = myRow.Item(4) ci.iID = docID
ci.iUrl = "Image.ashx?id=" & docID & "&database=" & docDB ci.iTitle = "test image" il.Add(ci) Next Return il End Function
Thanks for your help!
I've gotten further and I can actually see that the image is streaming in and I know it is grabbing it because at one point I wrote it out to a file using the ashx and verified the image was correct.
However, the image still doesn't display in the viewer. I realized all the images in the database are TIF images... is this the reason? Are only jpg/gif/png supported? I changed the contenttype at the top of the ashx to read image/tif but can't say I honestly understand what that does.
You are just missspelling the mime-type. The Image mime types (the content-type that should be emitted) are:
.tif image/tiff .jpg image/jpeg .gif image/gif .png image/png
Please note the "e" in JPEG and double F in TIFF. You can get a complete (or almost complete) reference of those mime types here: http://w3schools.sinsixx.com/media/media_mimeref.asp.htm