Checking if a Bitmap Array Contains a Specific Bitmao

D' Eradicated

Member
Joined
Aug 25, 2014
Messages
5
Programming Experience
1-3
Hello.

I'm having a problem with an array of bitmaps not returning true even if it contains the BackgroundImage of the PictureBox. Here is my code:

VB.NET:
Private Sub picGrid_Click(sender As Object, e As EventArgs) Handles pic77.Click, pic75.Click, pic73.Click, pic71.Click, pic66.Click, pic64.Click, pic62.Click, pic60.Click, pic57.Click, pic55.Click, pic53.Click, pic51.Click, pic46.Click, pic44.Click, pic42.Click, pic40.Click, pic37.Click, pic35.Click, pic33.Click, pic31.Click, pic26.Click, pic24.Click, pic22.Click, pic20.Click, pic17.Click, pic15.Click, pic13.Click, pic11.Click, pic06.Click, pic04.Click, pic02.Click, pic00.Click
        Dim picTile As PictureBox = sender
        Dim bmpBlueTiles() As Bitmap = {My.Resources.blue1, My.Resources.blue2, My.Resources.blue3, My.Resources.blue4, My.Resources.blue5, My.Resources.blue6, My.Resources.blue7, My.Resources.blue8, My.Resources.blue9, My.Resources.blue10, My.Resources.blue11, My.Resources.blue12}
        Dim bmpRedTiles() As Bitmap = {My.Resources.red1, My.Resources.red2, My.Resources.red3, My.Resources.red4, My.Resources.red5, My.Resources.red6, My.Resources.red7, My.Resources.red8, My.Resources.red9, My.Resources.red10, My.Resources.red11, My.Resources.red12}
        Dim X As Integer, Y As Integer


        If strCurrentPlayer = "blue" Then
            If bmpRedTiles.Contains(picTile.BackgroundImage) Then Exit Sub ' this line does not return to true, even if the array contains the picture

I tried looping through the array and checking if it matches but it didn't work. Any advice? Thanks in advanced.
 
When you use My.Resources, you aren't just being given a reference to an existing Image object. A new Image object is created and you are given a reference to that. That means that accessing the same property of My.Resources multiple times will generate multiple Image objects. That means that your array does NOT contain the same Image object as you have assigned to the BackgroundImage property of your PictureBox. To prove it to yourself, try this code:
If My.Resources.blue1 Is My.Resources.blue1 Then
    MessageBox.Show("Two references to one Image object.")
Else
    MessageBox.Show("Two Image objects.")
End If
What you should do is access each property of My.Resources once and only once, then use the resulting Image object multiple times. To do that, move your bmpBlueTiles and bmpRedTiles variables to the class level and then access the Image object only from there, including when you assign one to the BackgroundImage of the PictureBox.
 

Latest posts

Back
Top