Results 1 to 5 of 5

Thread: How to display image from db

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Angry How to display image from db

    hi guys, I would be grateful if anyone could help me on how I could display image from database. The images are saved in the db under categories, so I have a textbox which during the textcahnge display all categories having that text and display in a listbox, and I use bindingsource to filter the images according to the category selected from the listbox and the first image display and I use buttons to navigate forward and backwards but I have been struggling with how to display the filtered image from the bindingsource, so I would be grateful if anyone could help. I use the code below to display just one image but now would like to filter images under category and navigate through.

    Code:
    Private Sub ShowLabels()
            Dim arrPicture() As Byte = _
     CType(dsLabels.Tables(0).Rows(cboLabels.SelectedIndex)("LabelImage"), _
     Byte())
            Dim ms As New MemoryStream(arrPicture)
    
            With picImage
                .Image = Image.FromStream(ms)
                .SizeMode = PictureBoxSizeMode.Zoom
    
            End With
           
        End Sub
    Awaiting your responds. Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: How to display image from db

    shouldn't that be:

    vb Code:
    1. CType(dsLabels.Tables(0).Rows(cboLabels.SelectedIndex).item("LabelImage"), Byte())

    ?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to display image from db

    You say that you've got a BindingSource but you're not using it. Presumably you've bound the BindingSource to the ComboBox. Get the data from the BindingSource, which is what it's there for:
    vb.net Code:
    1. Dim row = DirectCast(myBindingSource.Current, DataRowView)
    2.  
    3. If row IsNot Nothing Then 'Allow for no item selected.
    4.     Dim data = TryCast(row("LabelImage"), Byte())
    5.  
    6.     If data IsNot Nothing Then 'Allow for DBNull.Value.
    7.         'etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Angry Re: How to display image from db

    Quote Originally Posted by jmcilhinney View Post
    You say that you've got a BindingSource but you're not using it. Presumably you've bound the BindingSource to the ComboBox. Get the data from the BindingSource, which is what it's there for:
    vb.net Code:
    1. Dim row = DirectCast(myBindingSource.Current, DataRowView)
    2.  
    3. If row IsNot Nothing Then 'Allow for no item selected.
    4.     Dim data = TryCast(row("LabelImage"), Byte())
    5.  
    6.     If data IsNot Nothing Then 'Allow for DBNull.Value.
    7.         'etc.
    Hi jmcihinney, thanks for your responds. I have manage to display image from the bindingsource using your code above but I can't navigate through a category which has more than 1 image.
    What the program does is, I filter the bindingsource and display the categories in a listbox, so when a category is selected from the listbox, if that category has more than 1 image you can navigate through by clicking nextbutton, previousbutton, firstbutton and lastbutton so each image and it's information are displayed. Now only the first image is displayed and I can't move to the next so if anyone could help. below are some part of the program.

    Code:
    Private Sub ShowCategoryNames()
            Dim MyRow As DataRowView, CurrentPicture As String, LastName As String
            If Me.txtCategories.Text <> "" Then
                If Me.chkCategory.Checked Then
                    Me.LoadPictures()
                    If Me.txtCategories.Text <> "Z" Then
                        dtView.Filter = "CatName >= '" + Me.txtCategories.Text + "' and CatName < '" + Chr(Asc(Me.txtCategories.Text) + 1) + "'"
                    Else
                        dtView.Filter = "CatName >= 'Z'"
                    End If
                    Me.BindingContext(dtView).Position = 0
                    LastName = ""
                    For Each MyRow In dtView
                        CurrentPicture = Trim(CStr(MyRow(("CatName"))))
                        If CurrentPicture <> LastName Then
                            Me.lstCategories.Items.Add(CurrentPicture)
                            LastName = CurrentPicture
                        End If
                    Next
                End If
            End If
        End Sub
    
     Private Sub txtCategories_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCategories.TextChanged
                           
     Me.ShowCategoryNames()
            
        End Sub
    
     Private Sub lstCategories_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCategories.SelectedIndexChanged
    
            Dim row = DirectCast(dtView.Current, DataRowView)
            
               dtView.Filter = "CatName = '" + CStr(lstCategories.SelectedItem) + "'"
            
    
            If row IsNot Nothing Then 'Allow for no item selected.
                Dim data = TryCast(row("LabelImage"), Byte())
                Dim ms As MemoryStream
                If data IsNot Nothing Then 'Allow for DBNull.Value.
                    ms = New MemoryStream(data)
                    Me.picImage.Image = Image.FromStream(ms)
                    ms.Close()
                End If
            End If
    
            Me.BindingContext(dtView).Position = 0
            Me.Size = New System.Drawing.Size(601, 509)
            Me.btnFirst.Enabled = Me.BindingContext(dtView).Count > 1
            Me.btnNext.Enabled = Me.BindingContext(dtView).Count > 1
            Me.btnPrevious.Enabled = Me.BindingContext(dtView).Count > 1
            Me.btnLast.Enabled = Me.BindingContext(dtView).Count > 1
    
          End Sub
    Now I need help on how I could the images and its text information when the bindingcontext position changes.
    Thanks
    Last edited by wiadus; Oct 20th, 2010 at 06:49 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Angry Re: How to display image from db

    Hi guys my code above when the bingingcontex changes the image does not display, it only work on when the bindingcontex position is 0. Can any one help. Thanks
    Last edited by wiadus; Oct 23rd, 2010 at 07:54 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width