Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
110
Images do not show up after adding to viewer
posted

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.

 

Parents
  • 4493
    Suggested Answer
    posted

    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.

    DynamicImages.zip
Reply Children