【问题标题】:Displaying a Image previous scanned or retrieved in Picturebox显示以前在 Picturebox 中扫描或检索的图像
【发布时间】:2017-11-21 18:45:13
【问题描述】:

我只是想为我的 RFID 考勤系统项目提供一些帮助。应扫描 RFID 标签以从数据库 mysql 中检索信息和图像。第一个学生将扫描他的 RFID 标签以在图片框 1 中显示他的图片,当另一个学生扫描图片框 1 时,图片框 1 将更新为新的学生图片和数据,前一个在图片框 2 上显示这里的问题是如何显示上一个学生的形象。只有图片,不包含数据。

我有一个 2 图片框

Picturebox1 是为新生扫描的 Picturebox2 是给上一个或者在新之前扫描的学生。

谢谢你们..任何建议和评论将不胜感激

这是我的代码

Private Sub studtag_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles studtag.TextChanged
    If studtag.TextLength = 8 Then


        con = New MySqlConnection
        con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
        Dim query As String

        query = "select * from dat.students"
        cmd = New MySqlCommand(query, con)



        Dim table As New DataTable

        Try
            con.Open()
            'Gets or sets an SQL statement or stored procedure used to select records in the database.
            With cmd
                .Connection = con
                .CommandText = "SELECT * from students where `studtags`='" & studtag.Text & "';"
            End With
            da.SelectCommand = cmd
            da.Fill(table)
            'it gets the data from specific column and fill it into textbox
            studtag.Text = table.Rows(0).Item(0)
            idno.Text = table.Rows(0).Item(1)
            lastxt.Text = table.Rows(0).Item(2)
            firstxt.Text = table.Rows(0).Item(3)
            middletxt.Text = table.Rows(0).Item(4)
            dob.Text = table.Rows(0).Item(6)

            crsetxt.Text = table.Rows(0).Item(10)

            tagtxt.Text = studtag.Text
            timein.Text = times.Text

            dr = cmd.ExecuteReader()
            dr.Read()

            If dob.Text = datenow.Text Then
                greet.Text = "Happy Birthday To You"

            End If



            Dim img() As Byte = CType(dr("studpic"), Byte())


            Using ms As New IO.MemoryStream(img)
                PictureBox1.Image = Image.FromStream(ms)

            End Using
            insert()
            loadtable()

        Catch ex As Exception
            Notenrolled.Show()
        Finally

            con.Dispose()
            con.Close()
        End Try

    End If

End Sub

Public Sub loadtable()

    con = New MySqlConnection
    con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
    Dim SDA As New MySqlDataAdapter
    Dim dbDataset As New DataTable
    Dim bSource As New BindingSource


    Try

        con.Open()
        Dim query3 As String

        query3 = "select studtags,idno,lastxt,firstxt,middletxt,dob,log,timein,crse from dat.studlogs"
        cmd = New MySqlCommand(query3, con)
        SDA.SelectCommand = cmd
        SDA.Fill(dbDataset)
        bSource.DataSource = dbDataset
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataset)
        DataGridView1.Sort(DataGridView1.Columns(8), System.ComponentModel.ListSortDirection.Ascending)
        If dbDataset.Rows.Count > 0 Then
            logins.Text = table2.Rows.Count.ToString()
        End If


        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        con.Dispose()
    End Try
End Sub

 Private Sub Students_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    loadtable()
    ''Date Now 
    Timer2.Start()


    Try
        DataGridView1.AllowUserToAddRows = False ' Disabled or hide (*) Symbol...

        DataGridView1.RowHeadersVisible = False 'To hide Left indicator..
        DataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue  'Selection backcolor....
        DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow 'Alternating Backcolor.
        DataGridView1.AllowUserToResizeRows = False 'Disabled  row resize...
        DataGridView1.ReadOnly = True
        DataGridView1.MultiSelect = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.ShowRowErrors = False
        DataGridView1.ShowCellErrors = False


        table2.Columns.Add("Student Tag", Type.GetType("System.String"))
        table2.Columns.Add("Student ID", Type.GetType("System.Int32"))
        table2.Columns.Add("Last Name", Type.GetType("System.String"))
        table2.Columns.Add("First Name", Type.GetType("System.String"))
        table2.Columns.Add("Middle Name", Type.GetType("System.String"))

        table2.Columns.Add("Status", Type.GetType("System.String"))
        table2.Columns.Add("Birthday", Type.GetType("System.String"))
        table2.Columns.Add("Time in", Type.GetType("System.String"))
        table2.Columns.Add("Course/Sec", Type.GetType("System.String"))


    Catch ex As Exception

    End Try

【问题讨论】:

  • 大部分代码似乎与所述问题无关。我看不到您试图将图像从一个移动到另一个的任何地方。我会使用一个变量,然后将其分配给另一个 PB...确保处理您不再需要的那些。
  • @Plutonix 对不起先生,我不知道从哪里开始,关于如何在每次新生扫描时将 Picturebox1 图像传输到 picturebox2。
  • 您的代码从字节创建一个图像并将其直接分配给 PB1。取而代之的是,将其分配给 Image 变量,以便您可以在需要时将 PB2.Image 设置为它。声明 2 个 img 变量可能更容易:currentStudentImgprevStudentImg 以使代码更易于阅读。您的应用程序已经泄漏,因此请务必处理掉它们

标签: mysql vb.net picturebox


【解决方案1】:

很简单,你可以用一个按钮来显示上一张图片:

  If datagridview1.CurrentRow.Index < datagridview1.Rows.Count Then
  datagridview1.Rows(datagridview1.SelectedRows(0).Index - 1).Selected 
             = True
 Dim pic As Byte()
 pic = datagridview1.CurrentRow.Cells(1).Value
 Dim ms As New MemoryStream(pic)
 pbox1.BackgroundImage = Image.FromStream(ms) 

【讨论】:

  • 很容易修复,告诉我哪一行显示错误
  • 无论如何...简单的解决方法是...尝试找出哪一行给出错误...找出该行后,确保您的连接字符串和datagridview填充代码在该行之前
  • okey,,,现在告诉我,你是在 datagridview 已经填充之后还是在 dgvw 填充之前单击按钮?如果你之前说过,那么就会发生错误。无论如何如果在填充dgvw之后发生错误,只需添加一个Try-Catch语句..不要使用Msgbox(ex.messgae),只需使用一个空的catch语句
  • 请记住,这段代码回到datagridview的上一行,确保你不在第一行,因为如果你已经在第一行,你怎么能去到上一行? Empt TRY-CATCH 语句将消除错误,但不会修复它..所以请确保您至少在第二行
  • 确保至少选择了第二行,然后您就可以转到上一行...并使用空的 TRY CATCH 语句来消除错误
猜你喜欢
  • 2015-06-29
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多