在表单上打印图像 - 跳过图像

时间:2014-07-25 17:15:58

标签: database vb.net image visual-studio-2012

我在vb中有一些代码用于将数据库中的图像(在以前的表单中添加)添加到新表单中。但是,当加载表单时,仅显示1或2个图像(取决于选择放入数据库中的图像)。我的逻辑有问题吗?以下是与相关表格相关的两个潜艇。

'Form2 is opened
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Go to the Form_Design sub
        Form_Design()

        'Connect to the database that is linked by connectionstring
        Using connection As New OleDbConnection(connectionstring)
            'Open the database connection
            connection.Open()

            'Variable for sql command that finds the current customer's ID
            Dim command1 As New OleDbCommand("SELECT CustomerID FROM Customers WHERE CustomerName = '" & Form1.txtID.Text & "'", connection)
            'Variable for reading the database data is set to the reader made from the sql command (command1)
            Dim Reader As OleDbDataReader = command1.ExecuteReader()

            'While the reader is reading the database data
            While Reader.Read()
                'Variable for storing the item that is in the 'CustomerID' column of the 'Customers' table from the database reader
                Dim CustID = Reader.Item("CustomerID")
                'Variable for sql command to count the number of images in the database for the current customer
                Dim command2 = "SELECT count(*) FROM Images WHERE CustomerID = @p1 AND DateTaken = @p2"

                'Connect to the database to prepare to execute command2
                Using selectcommand As New OleDbCommand(command2, connection)
                    'Add parameters to command2
                    'First parameter (@p1) is the ID of the customer (CustID)
                    selectcommand.Parameters.AddWithValue("@p1", CustID)
                    'Second paramter (@p2) is the date the image was taken (the date the user entered in the date picker on Form1)
                    selectcommand.Parameters.AddWithValue("@p2", Form1.DateTimePicker1.Value.Date.ToString)

                    'If there are less than 6 images in the database for the current customer then start the ImagesOnForm sub to display the images on the form
                        If selectcommand.ExecuteScalar() < 6 Then
                            ImagesOnForm()
                        End If
                End Using
            End While
        End Using
    End Sub     

'Sub for putting the images on the form
        Public Sub ImagesOnForm()
            'Connect to the database that is linked by connectionstring
            Using connection As New OleDbConnection(connectionstring)
                'Open the database connection
                connection.Open()

                'Variable for sql command that finds the current customer's ID
                Dim imagecommand1 As New OleDbCommand("SELECT CustomerID FROM Customers WHERE CustomerName = '" & Form1.txtID.Text & "'", connection)
                'Variable for reading the database data is set to the reader made from the sql command (command1)
                Dim ImagesReader As OleDbDataReader = imagecommand1.ExecuteReader()

                'While the reader is reading the database data
                While ImagesReader.Read()
                    'Variable for storing the item that is in the 'CustomerID' column of the 'Customers' table from the database reader
                    Dim CustID = ImagesReader.Item("CustomerID")

                    'Variable to read the images in the database for the current customer
                    Dim locationcommand = "SELECT * FROM Images WHERE CustomerID = @p1 AND DateTaken = @p2"

                    'Connect to the database to prepare to read the images in the database for the current customer
                    Using secondselectcommand As New OleDbCommand(locationcommand, connection)
                        'Add paramters to locationcommand
                        'First parameter (@p1) is the ID of the customer (CustID)
                        secondselectcommand.Parameters.AddWithValue("@p1", CustID)
                        'Second parameter (@p2) is the date the image was taken (the date the user entered in the date picker on Form1)
                        secondselectcommand.Parameters.AddWithValue("@p2", Form1.DateTimePicker1.Value.Date.ToString)

                        'Variable for reading the database data is set to the reader made from the sql command (secondselectcommand)
                        Dim rdr As OleDbDataReader = secondselectcommand.ExecuteReader()
                        'Variable for the x coordinate of the picture boxes
                        Dim w As Integer = (Me.Width / 24)
                        'Variable for the y coordinate of the picture boxes
                        Dim h As Integer = (Me.Height / 6)

                        'While the reader is reading the database data:
                        While rdr.Read()
                            'For each and every image in the database for the current customer:
                            For Each i In rdr
                                'Variable for the item in the 'ImageLocation' column of the Images table from the reader
                                Dim image = rdr.Item("ImageLocation")
                                'Variable for each new picture box
                                Dim p As New PictureBox

                                'Picture box properties
                                'Makes the image fit to the picture box, but without changing its size ratio
                                p.SizeMode = PictureBoxSizeMode.Zoom
                                'Width of the picture box is 1/4 of the form width
                                p.Width = (Me.Width / 4)
                                'Height of the picture box is 1/4 of the form height
                                p.Height = (Me.Height / 4)

                                'Test
                                MsgBox(image)

                                'Sets the location of the picturebox to the coordinates made from the previously declared x and y positions (w and h)
                                p.Location = New Point(w, h)
                                'Add the pictue box to the form in the specified location - (w,h)
                                Me.Controls.Add(p)
                                'Sets the image of the picture box as the image that was read from the database (rdr)
                                p.ImageLocation = image

                                'Text box to display the letter of the group on each image
                                Dim pText As New TextBox
                                'Sets position of text box to be on top of the image
                                pText.Location = p.Location

                                'Fitting images into their respective groups based on their dimensions
                                If (p.Image.Width / p.Image.Height) = 1.5 Or (p.Image.Height / p.Image.Width) = 1.5 Then
                                    pText.Text = "A"
                                ElseIf (p.Image.Width / p.Image.Height) = 1 Or (p.Image.Height / p.Image.Width) = 1 Then
                                    pText.Text = "B"
                                ElseIf (p.Image.Width / p.Image.Height) = 3 Or (p.Image.Height / p.Image.Width) = 3 Then
                                    pText.Text = "C"
                                ElseIf (p.Image.Width / p.Image.Height) = 1.4 Or (p.Image.Height / p.Image.Width) = 1.4 Then
                                    pText.Text = "D"
                                ElseIf (p.Image.Width / p.Image.Height) = 1.25 Or (p.Image.Height / p.Image.Width) = 1.25 Then
                                    pText.Text = "E"
                                ElseIf (p.Image.Width / p.Image.Height) = (4 / 3) Or (p.Image.Height / p.Image.Width) = (4 / 3) Then
                                    pText.Text = "F"
                                ElseIf (p.Image.Width / p.Image.Height) = 1.2 Or (p.Image.Height / p.Image.Width) = 1.2 Then
                                    pText.Text = "G"
                                ElseIf (p.Image.Width / p.Image.Height) = (5 / 3) Or (p.Image.Height / p.Image.Width) = (5 / 3) Then
                                    pText.Text = "H"
                                Else
                                    pText.Text = "I"
                                End If

                                'If the next picture box doesn't fit next to the previous one then put it on the next line
                                If w + (2 * p.Width) > Me.Width Then
                                    'Sets the y coordinate to be the curent height + height of the picture box + 1/12 of the form height
                                    h += p.Height + (Me.Height / 6)
                                    'Sets the x coordinate back to where it began (start of new line, as it is now lower due to h being changed above)
                                    w = (Me.Width / 24)
                                Else
                                    'Sets the x coordinate of the picturebox to the position next to the previous picture box
                                    w += p.Width + (Me.Width / 12)
                                End If

                                'Test
                                MsgBox("Loop Complete")
                            Next i
                        End While
                    End Using
                End While
            End Using
        End Sub

0 个答案:

没有答案