从数据库中检索图像?

时间:2014-05-12 17:44:22

标签: sql vb.net winforms visual-studio-2012

我是论坛的新手,想问一个多年来困扰我的问题。我在Windows Forms中使用Visual Studio Express 2012。

我希望拥有一个由不同图像组成的数据库。每行都有自己的图像,行中的其他字段定义图像特征(I.E.Color = Red,Striped = Yes等)及其自己的特定ID。

现在我想要做的是允许用户通过表单进行搜索(根据表单上的选项选择他们希望图像具有哪些特征,然后使用SQL语句根据输入检索图像)。我遇到的唯一问题是在搜索时显示表单上的所有图像?有没有想过如何动态地这样做?

2 个答案:

答案 0 :(得分:1)

我在其中创建了一个包含FlowLayoutPanel的表单。我将其AutoScroll属性设置为true,这样如果有更多的图片不适合空间,它将显示一个滚动条,以便您可以全部看到它们。

我不完全确定你是如何获取图像的,但假设你有一个返回图像列表的函数。

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

然后您可以使用以下函数动态创建要添加到PictureBox的{​​{1}}控件。

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 的大小设置为图像的大小。你可能想尝试缩放它们或制作缩略图,但我会把它留给你(或另一个问题)。您可能还想要另一种方法来清除图像。

PictureBox

我不相信最后一种方法是完全正确的,但你可能想要一些接近它的方法。

答案 1 :(得分:0)

试试这个,

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
相关问题