2015-01-07 48 views
1

我不知道,如果它的時間已晚,但我已經是以下陣列上For循環:For Loop工作不正常,或者發生錯誤。當工作條件顛倒

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4} 

    For i As Integer = 0 To pboxes.Count - 1 
     If pboxes(i).Image Is My.Resources.list Then 
      pboxes(i).Image = Nothing 
     End If 
    Next 
End Sub 

環路應該檢查是否有任何的圖片框在數組有一個名爲List的Image存儲在它們的Resources文件夾中。如果是,請將圖像設置爲無。但是,我運行它並沒有任何反應,沒有錯誤,沒有任何反應。

所以我扭轉我的For循環如下,看看會發生什麼:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4} 

    For i As Integer = 0 To pboxes.Count - 1 
     If pboxes(i).Image Is Nothing Then 
      pboxes(i).Image = My.Resources.list 
     End If 
    Next 
End Sub 

這工作,但不是我想要的,我想要的正好相反。

我在這裏做錯了什麼?

+0

什麼是My.Resources.list? – Bharadwaj

+0

@Bharadwaj My.Resources.List是我保存在我的資源文件中的圖像 –

+2

每當您訪問資源中的圖像時,都會創建一個**副本**。因此,您的PictureBox中的圖像永遠不會「等於」資源中的圖像。我建議你以其他方式跟蹤PictureBox中的圖像。一旦可能性將簡單的字符串值放入PictureBox的Tag屬性中。 –

回答

1

一種選擇是,如果你在框中設置的圖像編程,設置My.Resources.list通過一個全局變量引用,即Public pbList = My.Resources.list

然後,當您最初設置的畫面,使用該變量,所以:picMainImage.Image = pbList

最後,在If聲明,則應能夠檢查If pboxes(i) is pbList Then...

一旦它變成一個變量,它似乎變成靜態的,因此無論你在哪裏使用它,它都將是一樣的。

編輯:我用了幾個月前的一些實際代碼:在子

If imgpath <> "" Then 
    Me.lblImg.ImageLocation = imgpath 
Else 
    Me.lblImg.ImageLocation = Nothing 
    Me.lblImg.Image = pbimage 
End If 

在模塊(外分)

Public pbimage As System.Drawing.Image = My.Resources.placeholder 

然後再這是我使用的所有圖片沒有問題(它是我單擊圖像時運行的功能 - 如果它的佔位符,那麼您可以瀏覽圖像並將其保存到數據文件夾,否則它會執行n沒有)

Private Sub changeImg(sender As Object, e As MouseEventArgs) Handles {ALL YOUR IMAGES}.Click 
    If TypeOf sender Is PictureBox Then 
     If DirectCast(sender, PictureBox).Image Is pbimage Then 
      Dim ofd As New OpenFileDialog 
      ofd.Title = "Please select image" 
      ofd.Filter = "Image Files|*.jpg" 
      If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then 
       Dim rn As New Random 
       Dim r As Long = rn.Next(111111, 999999) 
       Dim newfilename As String = My.Settings.dataPath & r.ToString & Format(Now, "ddmmyy") & ".jpg" 
       Try 
        FileCopy(ofd.FileName, newfilename) 
        DirectCast(sender, PictureBox).ImageLocation = newfilename 
       Catch ex As Exception 
        MessageBox.Show("Check permissions to the Data folder", "Permissions error") 
       End Try 
      End If     
     End If 
    End If 
End Sub 
+0

你測試過了嗎?請用一些示例代碼更新您的答案。我最初想要嘗試這個,但它不起作用。我很好奇看到這個工作,因爲它比比較像素更簡單。 –

+0

我在幾個月前寫過的程序中遇到了完全相同的問題,並將其用作解決方案。當你加載應用程序而不是聲明它時,你可能不得不聲明pbList,但一旦它被設置,它應該按預期工作。現在在我的手機上,但會嘗試挖掘我使用的代碼,當我有機會。 – bmgh1985

+0

@JoeyJoeJoeJrShabadoo現在已添加該示例。 – bmgh1985

1

圖像無法進行比較,因爲圖像被複制到內存中,即使像素匹配,圖像也會始終不同。直接比較像素以確定圖像是否相同。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4} 

    For i As Integer = 0 To pboxes.Count - 1 
     If IsSameImage(pboxes(i).Image, My.Resources.list) = True Then 
      pboxes(i).Image = Nothing 
     End If 
    Next 
End Sub 

Public Function IsSameImage(ByVal oBitmap1 As Bitmap, ByVal oBitmap2 As Bitmap) As Boolean 
    For x = 0 To oBitmap1.Width - 1 
     For y = 0 To oBitmap2.Height - 1 
      If Not oBitmap1.GetPixel(x, y) = oBitmap2.GetPixel(x, y) Then 
       Return False 
      End If 
     Next 
    Next 

    Return True 
End Function