user3629521
user3629521

Reputation: 1

Retrieving images from a database?

I'm new to the forums and wanted to ask a question that has been bugging me for ages. I am using Visual Studio express 2012 with Windows Forms.

I want to have a database consisting of different images. Each row has its own image, and the other fields in the row define the images characteristics (I.E. Colour = Red, Striped = Yes etc) and its own specific ID.

Now what I want to do is allow the user to search via the form (Selecting what characteristics they want the image to have based on options on the form and then using SQL statements to retrieve the images based on their inputs). The only issue I am having is displaying all of the images on the form when they have searched? Is there any idea as to how to do this dynamically?

Upvotes: 0

Views: 145

Answers (2)

Jeff B
Jeff B

Reputation: 9002

I created a form with FlowLayoutPanel in it. I set its AutoScroll property to true so that if there more pictures than fit in the space, it'll show a scroll bar so you can see them all.

I'm not entirely sure how you're getting the image, but assuming you have a function that returns a list of images.

Private Function DoImageSearch(parameters As SearchParameters) As List(Of Image)
    'Go get images from database
End Function

Then you could have a function like the following to dynamically create PictureBox controls to be added to the FlowLayoutPanel.

Private Sub DynamicallyCreatedPictureBoxes(images As List(Of Image))
    For Each image In images
        Dim picture = New PictureBox()
        picture.Image = image
        picture.Size = image.Size
        FlowLayoutPanel1.Controls.Add(picture)
    Next
End Sub

In this case I've set the size of the PictureBox to be the size of the image. You may want to try to scale them or make thumbnails, but I'll leave that up to you (or another question). You'd probably also want another method to clear the images.

Private Sub ClearPictureBoxes()
    FlowLayoutPanel1.SuspendLayout()

    For Each control As Control In FlowLayoutPanel1.Controls
        control.Dispose()
    Next

    FlowLayoutPanel1.Controls.Clear()

    FlowLayoutPanel1.ResumeLayout()
End Sub

I'm not confident that last method is entirely correct, but you'd probably want something close to it.

Upvotes: 1

King of kings
King of kings

Reputation: 695

Try like the this,

If color_RadioButton.Checked = True and type_RadioButton.Checked = True Then
    cmdTect="color='" & color_RadioButton.Text & "' and type='" & type_RadioButton.Text & "'"
ElseIf color_RadioButton.Checked = True and type_RadioButton.Checked = False Then
    cmdTect="color='" & color_RadioButton.Text & "'"
ElseIf color_RadioButton.Checked = False and type_RadioButton.Checked = TrueThen
    cmdTect="type='" & type_RadioButton.Text & "'"
End If
dim cmd as new sqlcommand("select count(photo) from tbl where " & cmdTect,conn)
dim cnt as integer=cmd.ExecuteScalar()
for i as integer=0 to cnt
    cmd = New SqlCommand("select photo from tbl where" & cmdTect,conn)
    Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
    If Not imageData Is Nothing Then
        Dim ms As New MemoryStream(imageData, 0, imageData.Length)
        ms.Write(imageData, 0, imageData.Length)
        Dim pic As New PictureBox
        pic.BackgroundImage = Image.FromStream(ms, True)
        Panel1.Controls.Add(pic)
    End If
Next

Upvotes: 0

Related Questions