【问题标题】:Retrieving images from a database?从数据库中检索图像?
【发布时间】:2014-05-12 17:44:21
【问题描述】:

我是论坛的新手,想问一个困扰我多年的问题。我正在使用带有 Windows 窗体的 Visual Studio express 2012。

我想要一个包含不同图像的数据库。每行都有自己的图像,行中的其他字段定义图像特征(即颜色 = 红色、条纹 = 是等)及其自己的特定 ID。

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

【问题讨论】:

  • 我很困惑为什么如果用户搜索Red, Stripes, Circle,所有图像都必须显示
  • 对不起,我没有说清楚!我只想显示与用户选择的内容相关的图像(假设他们选择红色和圆圈,然后我希望它只显示红色和有圆圈的图像,而不是所有图像。抱歉。
  • 那么,The only issue I am having is displaying all of the images on the form when they have searched? 的意思是显示在搜索中找到的图像?你试过什么?它们会放在图片框中吗?
  • 你在做什么风格的用户界面? WPF,WinForms?如果您编辑帖子并添加适当的标签,这将很有帮助,以便我们知道。如果您使用的是 WinForms,带有动态数量的 PictureBox 控件的 FlowLayoutPanel 可能会起作用。
  • @Plutonix 我能够使用 SQL 查询获得第一张显示在独立图片框中的图像; (SELECT ImageLocation FROM Kits WHERE Colour='Red' AND Circles='Yes'),然后使用数据读取器读取值并存储文件路径 (ImageLocation)。然后将此变量放入图片框代码中。我实际上是在问如何让所有匹配的搜索结果重复此操作?也许使用 SQL COUNT 函数然后使用 Do Loop 直到 count=0?

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


【解决方案1】:

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

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

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

然后您可以使用如下函数来动态创建PictureBox 控件以添加到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 的大小设置为图像的大小。您可能想尝试缩放它们或制作缩略图,但我会留给您(或其他问题)。您可能还需要另一种方法来清除图像。

Private Sub ClearPictureBoxes()
    FlowLayoutPanel1.SuspendLayout()

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

    FlowLayoutPanel1.Controls.Clear()

    FlowLayoutPanel1.ResumeLayout()
End Sub

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

【讨论】:

    【解决方案2】:

    试试这个,

    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
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2014-03-02
      • 2014-07-20
      • 2023-03-21
      相关资源
      最近更新 更多