簡體   English   中英

vb.net一張一張地將圖像設置為圖片框

[英]vb.net set images to picturebox one by one

我在vb.net代碼中執行了以下步驟:

  1. 從文本文件中獲取包含所需值的行,以我為例,我需要最后一個值作為“組”

  2. 創建一個圖片框數組,圖片框的數量是步驟1中的數量。1行有2個圖片框。

之后,我想將從文本文件中讀取的圖像設置為1到1的圖片框(圖片名稱是2nd value.jpg),但是我沒有找到正確的方法。

有什么建議嗎?

我的文本文件如下:

在此處輸入圖片說明

到目前為止,我的代碼是:

    Private PicBoxArray(9) As PictureBox        

    Dim value0 As String
    Dim value1 As String
    Dim value2 As String
    Dim value3 As String

    Dim fileName As String = "D:\local\MyTest1.txt"
    Dim subString1 As String = "Group"
    Dim lines As String() = File.ReadAllLines(fileName)
    For i As Integer = 0 To lines.Length - 1
        If lines(i).Contains(subString1) Then
            count1 += 1
    end if
    Next

    For i = 1 To count1
        PicBoxArray(i) = New PictureBox
        With PicBoxArray(i)
            .Tag = i
            .Size = New Size(330, 280)
            If i Mod 2 = 0 Then
                .Location = New Point(430, 80 + 300 * ((i - 2) / 2))                   
            Else
                .Location = New Point(80, 80 + 300 * ((i - 1) / 2))
            End If

            .Parent = Me
            .Visible = True
        End With
    Next

  Using MyReader As New Microsoft.VisualBasic.
                FileIO.TextFieldParser("D:\PUB_GIS_Tagging\local\MyTest.txt")

        Dim lineCount = File.ReadAllLines("D:\PUB_GIS_Tagging\local\MyTest.txt").Length

        MyReader.TextFieldType = FileIO.FieldType.Delimited
        MyReader.SetDelimiters(",")
        Dim currentRow As String()
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()

                value0 = currentRow(0)  
                value1 = currentRow(1)  
                value2 = currentRow(2) 
                value3 = currentRow(3)


   If value3 = "Group"
       '======= put the picture from PicBoxArray(1) to PicBoxArray(count1) i by 1========
       '====================== I am stuck here=============
   End if 

   Catch ex As Microsoft.VisualBasic.
         FileIO.MalformedLineException
         MsgBox("Line " & ex.Message &
                "is not valid and will be skipped.")
   End Try
 End While
End Using

我假設您想從文件中讀取一行,然后將該行中的圖片設置為您的第一個圖片框,然后閱讀下一行並將下一張圖片設置為下一個圖片框?

Dim linecounter as Integer = 0 
While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()

            value0 = currentRow(0)  
            value1 = currentRow(1)  
            value2 = currentRow(2) 
            value3 = currentRow(3)


If value3 = "Group" Then
    PicBoxArray(linecounter).ImageLocation = "C:\vb_test\" & value1 & ".pdf.jpg" 
    linecounter += 1
End if 
next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM