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
Re: How to display image from db
shouldn't that be:
vb Code:
CType(dsLabels.Tables(0).Rows(cboLabels.SelectedIndex).item("LabelImage"), Byte())
?
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:
Dim row = DirectCast(myBindingSource.Current, DataRowView)
If row IsNot Nothing Then 'Allow for no item selected.
Dim data = TryCast(row("LabelImage"), Byte())
If data IsNot Nothing Then 'Allow for DBNull.Value.
'etc.
Re: How to display image from db
Quote:
Originally Posted by
jmcilhinney
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:
Dim row = DirectCast(myBindingSource.Current, DataRowView)
If row IsNot Nothing Then 'Allow for no item selected.
Dim data = TryCast(row("LabelImage"), Byte())
If data IsNot Nothing Then 'Allow for DBNull.Value.
'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
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