计算并限制使用VB.NET上传的文件数

时间:2021-07-07 14:01:42

I want to take input of 5 images from user, show thumbnails and save them to 5 columns in SQL-database(image1,image2,image3,image4,image5).

我想从用户输入5个图像,显示缩略图并将它们保存到SQL数据库中的5列(image1,image2,image3,image4,image5)。

Please add code for sql insertion of these 5 files.

请为这5个文件的sql插入添加代码。

Please help me out

请帮帮我

   Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
     "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
     "All files (*.*)|*.*"

    OpenFileDialog1.Multiselect = True
    Dim index As New Integer

    OpenFileDialog1.Title = "Select Photos"

    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        For Each file As String In OpenFileDialog1.FileNames
            Dim imageControl As New PictureBox()
            imageControl.Height = 100
            imageControl.Width = 100
            Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            Dim myBitmap As New Bitmap(file)
            Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
            imageControl.Image = myThumbnail

            PhotoGallary.Controls.Add(imageControl)
            index = OpenFileDialog1.FilterIndex()
        Next
        If index > 5 Then
            MessageBox.Show("Please select no more than 5 files")
        Else
            btn_Save.Enabled = True

        End If
    End If
End Sub

2 个解决方案

#1


If you only want to allow 5 image files, there is no need to bother making thumbnails before testing the number of files. Since FileDialog.FileNames Property returns an array of strings, the first thing you want to do after testing that the DialogResult is Ok, is test the length of the array:

如果您只想允许5个图像文件,则在测试文件数量之前无需费心缩略图。由于FileDialog.FileNames属性返回一个字符串数组,因此在测试DialogResult为Ok之后要做的第一件事就是测试数组的长度:

If OpenFileDialog1.FileNames.Length > 5 Then 
    MessageBox.Show("Please select no more than 5 files")
    Exit sub
End If
'' The rest of your code, including the row btn_Save.Enabled = True.

#2


Problem Solved!

Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
     "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
     "All files (*.*)|*.*"

    OpenFileDialog1.Multiselect = True
    Dim index As New Integer

    OpenFileDialog1.Title = "Select Photos"

    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        If OpenFileDialog1.FileNames.Length > 5 Then
            MessageBox.Show("Please select no more than 5 files")
            Exit Sub
        End If
        If OpenFileDialog1.FileNames.Length < 5 Then
            MessageBox.Show("Please select 5 files")
            Exit Sub
        End If
        For Each file As String In OpenFileDialog1.FileNames
            Dim imageControl As New PictureBox()
            imageControl.Height = 100
            imageControl.Width = 100
            Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            Dim myBitmap As New Bitmap(file)
            Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
            imageControl.Image = myThumbnail

            PhotoGallary.Controls.Add(imageControl)
            index = OpenFileDialog1.FilterIndex()
        Next
        btn_Save.Enabled = True
        BrowseMultipleFilesButton.Enabled = False


    End If
End Sub

#1


If you only want to allow 5 image files, there is no need to bother making thumbnails before testing the number of files. Since FileDialog.FileNames Property returns an array of strings, the first thing you want to do after testing that the DialogResult is Ok, is test the length of the array:

如果您只想允许5个图像文件,则在测试文件数量之前无需费心缩略图。由于FileDialog.FileNames属性返回一个字符串数组,因此在测试DialogResult为Ok之后要做的第一件事就是测试数组的长度:

If OpenFileDialog1.FileNames.Length > 5 Then 
    MessageBox.Show("Please select no more than 5 files")
    Exit sub
End If
'' The rest of your code, including the row btn_Save.Enabled = True.

#2


Problem Solved!

Private Sub BrowseMultipleFilesButton_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles BrowseMultipleFilesButton.Click
    Dim OpenFileDialog1 As New OpenFileDialog
    OpenFileDialog1.Filter =
     "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
     "All files (*.*)|*.*"

    OpenFileDialog1.Multiselect = True
    Dim index As New Integer

    OpenFileDialog1.Title = "Select Photos"

    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        If OpenFileDialog1.FileNames.Length > 5 Then
            MessageBox.Show("Please select no more than 5 files")
            Exit Sub
        End If
        If OpenFileDialog1.FileNames.Length < 5 Then
            MessageBox.Show("Please select 5 files")
            Exit Sub
        End If
        For Each file As String In OpenFileDialog1.FileNames
            Dim imageControl As New PictureBox()
            imageControl.Height = 100
            imageControl.Width = 100
            Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            Dim myBitmap As New Bitmap(file)
            Dim myThumbnail As Image = myBitmap.GetThumbnailImage(96, 96, myCallback, IntPtr.Zero)
            imageControl.Image = myThumbnail

            PhotoGallary.Controls.Add(imageControl)
            index = OpenFileDialog1.FilterIndex()
        Next
        btn_Save.Enabled = True
        BrowseMultipleFilesButton.Enabled = False


    End If
End Sub