Results 1 to 15 of 15

Thread: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

  1. #1

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Resolved [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    I'm trying to check if the background image of a clicked button equals the first entry in an imagelist. I'm using code found on forums as the answer to 3 different inquiries, but it's not working.
    Code:
        Private MyImage As Image
        Private Button As Integer
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            For Each B As Control In FlowLayoutPanel1.Controls
                If TypeOf B Is Button Then
                    AddHandler B.Click, AddressOf Button_Click
                End If
            Next
            ImageList1.Images.Add(My.Resources.Fox)
            ImageList1.Images.Add(My.Resources.Chicken1)
            ImageList1.Images.Add(My.Resources.Chicken2)
            ImageList1.Images.Add(My.Resources.Chicken3)
            ImageList1.Images.Add(My.Resources.Chicken4)
            ImageList1.Images.Add(My.Resources.Chicken5)
            ImageList1.Images.Add(My.Resources.Chicken6)
        End Sub
    
        Private Sub Pickbutton()
            button = rand.Next(1, 50)
        End Sub
    
     Private Sub PickImage()
            Dim colr As Integer
            Dim randcolr As New Random
            colr = randcolr.Next(0, 7)
            MyImage = ImageList1.Images(colr)
     End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Pickbutton()
            PickImage()
            Select Case Button
                Case 1
                    Button1.BackgroundImage = MyImage
                Case 2
                    Button2.BackgroundImage = MyImage
                Case 3
                    ...etc
            End Select
       End Sub
    
        Private Sub Button_Click(ByVal sender As Object, e As System.EventArgs)
            Dim B As Button = CType(sender, Button)
            'Dim I As Image = My.Resources.Fox
                MsgBox(B & " clicked.")   '<<<< This fires okay
            If B.BackgroundImage Is ImageList1.Images(0) Then '<<< Code from Web
                MsgBox("Fox clicked!")   '<<<<This does not fire
                B.BackgroundImage = Nothing
                Score()
            Else
                SetSpeed()
                Exit Sub
            End If
        End Sub
    *Edit...
    I realized I ignored "I as image" because I was trying different things to get it to work. The following doesn't work either.
    Code:
            Dim I As Image = ImageList1.Images(0)
            PictureBox1.Image = B.BackgroundImage '<<< This correctly shows the current background image.
            If B.BackgroundImage Is I Then
    Also, I have verified that ImageList.Images(0) is the fox with PictureBox1.Image = ImageList.Images(0)
    Last edited by Amerigoware; Nov 30th, 2021 at 09:30 AM. Reason: other methods attemped

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [vb.net 2021] [core 6.0] comparing images

    Since you have control over all the parts, I would suggest that you not try to solve the problem this way. There is always going to be an issue with images that you'll have to be careful to avoid. If you end up with two copies, such that there are two regions of memory holding the same image, they won't be the same. All that you are holding is the address of the memory where the image is found, so if you have two copies, they might look the same when displayed, but be different as far as the computer is concerned. Whether or not that's an issue here barely matters, because there's a better way to handle this.

    Every control, including a picturebox, button, or anything else, has a .Tag property. That property is type Object, and is there so you can associate whatever you want with a control. So, you could put a string in there, such as the name of the resource. Checking whether the .Tag.ToString = "whatever you want to look for" is going to be more reliable than checking the images. You could also put an integer into the .Tag property and switch on that if you didn't want to deal with comparing strings. Lots of options there. Use the .Tag for whatever you find most convenient.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: [vb.net 2021] [core 6.0] comparing images

    Thanks for the suggestion. After some rearranging of code, I finally got it working with tags.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [vb.net 2021] [core 6.0] comparing images

    There's not a VB.Net 2021 is there? I got 2022 Preview, but no 2021

    Here's a couple of code improvements for you...

    Code:
    For Each B As Button In FlowLayoutPanel1.Controls.OfType(Of Button)
        AddHandler B.Click, AddressOf Button_Click
    Next
    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Pickbutton()
        PickImage()
        FlowLayoutPanel1.Controls("Button" & Button).BackgroundImage = MyImage
    End Sub

  5. #5

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: [vb.net 2021] [core 6.0] comparing images

    Oof. Yeah, I have 2022 (Current). Thanks for the code. With some changes I've made, it doesn't work with changing the image of a picturebox, although it would for the backgroundimage. I'm sure it might if I insert a ctype in there.

  6. #6

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Cool Re: [vb.net 2022] [core 6.0] comparing images

    Now, I'm not so sure. Anyway, it doesn't matter. My messy code is working perfectly.

    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
        Private MPrevPos As New Point
        Private Hue As New Color
        Private ReadOnly R As New Random
        Private Rnum As Integer
        Private Button As Integer
        Private ReadOnly Rand As New Random
        Private Speed As Integer
        Private N As Integer = 5
        Private MyImage As Image
        Private MyTag As String
        Private Pictures As Image()
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Location = My.Settings.MyLocation
            Pictures = {My.Resources.Fox, My.Resources.Chicken1, My.Resources.Chicken2, My.Resources.Chicken3,
                My.Resources.Chicken4, My.Resources.Chicken5, My.Resources.Chicken6}
            UseImageChk.Checked = My.Settings.UseImages
            If UseImageChk.Checked Then
                Label1.Text = ("Click the fox " & (N) & " times to exit.")
                Dim Img As Image = My.Resources.Fox
                Dim ico As System.Drawing.Icon = System.Drawing.Icon.FromHandle(My.Resources.Fox.GetHicon())
                Me.Icon = ico
            Else
                Label1.Text = ("Click the red button " & (N) & " times to exit.")
                Me.Icon = My.Resources.Icon
            End If
    
            For I = 1 To 49
                Dim Pic As New PictureBox With {
                    .BackColor = Color.Transparent,
                    .Name = "Button" & I,
                    .Size = New Size(52, 52),
                    .SizeMode = PictureBoxSizeMode.StretchImage,
                    .Cursor = New Cursor(New IO.MemoryStream(My.Resources.Target))
                }
                If UseImageChk.Checked Then
                    PickImage()
                    Pic.Image = MyImage
                    Pic.Tag = MyTag
                Else
                    Pic.BackColor = PickColor()
                End If
    
                FlowLayoutPanel1.Controls.Add(Pic)
            Next
            FlowLayoutPanel1.Cursor = New Cursor(New IO.MemoryStream(My.Resources.Target))
            For Each B As Control In FlowLayoutPanel1.Controls
                If TypeOf B Is PictureBox Then
                    AddHandler B.Click, AddressOf Button_Click
                End If
            Next
            Speed = Timer1.Interval
            Timer1.Enabled = True
            Timer1.Start()
        End Sub
    
        Private Sub CloseApp()
            Timer1.Stop()
            My.Settings.MyLocation = Me.Location
            My.Settings.UseImages = UseImageChk.Checked
            My.Settings.Save()
            Application.Exit()
            Me.Close()
            End
        End Sub
    
        Private Sub Pickbutton()
            button = rand.Next(1, 50)
        End Sub
    
        Private Sub PickImage()
            Dim colr As Integer
            Dim randcolr As New Random
            colr = randcolr.Next(0, 7)
            If colr = 0 Then
                Rnum = R.Next(1, 100)
                If Rnum < 95 Then
                    colr = R.Next(1, 7)
                    MyImage = Pictures(colr)
                    MyTag = "Chicken"
                    Exit Sub
                Else
                    MyImage = Pictures(colr)
                    MyTag = "Fox"
                    Timer2.Enabled = True
                    Timer2.Start()
                    Dim count2 As New Stopwatch
                    count2.Start()
                    'Do Until count2.Elapsed.Seconds = 1
                    If count2.Elapsed.Seconds = 1 Then
                        Hue = Color.White
                        count2.Stop()
                        count2.Reset()
                        Timer2.Stop()
                        Timer2.Enabled = False
                    End If
                    'Loop
    
                End If
            Else
                MyImage = Pictures(colr)
                MyTag = "Chicken"
    
            End If
        End Sub
    
        Private Function PickColor() As Color
            Dim colr As Integer
            Dim randcolr As New Random
            colr = randcolr.Next(0, 7)
    
            If colr = 0 Then
                Rnum = R.Next(1, 100)
                If Rnum < 95 Then
                    Return Color.Orange
                Else
                    Hue = Color.Red
                    Timer2.Enabled = True
                    Timer2.Start()
                    Dim count2 As New Stopwatch
                    count2.Start()
                    'Do Until count2.Elapsed.Seconds = 1
                    If count2.Elapsed.Seconds = 1 Then
                        Hue = Color.White
                        count2.Stop()
                        count2.Reset()
                        Timer2.Stop()
                        Timer2.Enabled = False
                    End If
                    'Loop
                End If
                Return Hue
            Else
                If colr = 1 Then Hue = Color.Orange
                If colr = 2 Then Hue = Color.Yellow
                If colr = 3 Then Hue = Color.Green
                If colr = 4 Then Hue = Color.Blue
                If colr = 5 Then Hue = Color.Indigo
                If colr = 6 Then Hue = Color.Violet
                Return Hue
            End If
        End Function
    
        Private Sub Score()
            N -= 1
            Select Case UseImageChk.Checked
                Case True
                    Label1.Text = ("Click the fox " & (N) & " times to exit.")
                Case False
                    Label1.Text = ("Click the red button " & (N) & " times to exit.")
            End Select
    
            If N = 0 Then
                CloseApp()
            End If
        End Sub
    
        Private Sub SetSpeed()
            If speed > 20 Then
                speed = Timer1.Interval - 20
                Timer1.Interval = speed
    
            End If
        End Sub
    
        Private Sub Button_Click(ByVal sender As Object, e As System.EventArgs)
            Dim B As PictureBox = CType(sender, PictureBox)
            If UseImageChk.Checked Then
                If B.Tag.ToString = "Fox" Then
                    PickImage()
                    B.Image = MyImage
                    B.Tag = MyTag
                    Score()
                Else
                    SetSpeed()
                    Exit Sub
                End If
            Else
                If B.BackColor = Color.Red Then
                    B.BackColor = PickColor()
                    Score()
                Else
                    SetSpeed()
                    Exit Sub
                End If
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Pickbutton()
            If UseImageChk.Checked Then
                For Each Itm As PictureBox In FlowLayoutPanel1.Controls
                    If Itm.Name = "Button" & Button Then
                        PickImage()
                        Itm.Image = MyImage
                        Itm.Tag = MyTag
                    End If
                Next
    
            Else
    
                FlowLayoutPanel1.Controls("Button" & Button).BackColor = PickColor()
            End If
        End Sub
    
        Private Sub UseImageChk_CheckedChanged(sender As Object, e As EventArgs) Handles UseImageChk.CheckedChanged
            If UseImageChk.Checked Then
                Label1.Text = ("Click the fox " & (N) & " times to exit.")
                Dim Img As Image = My.Resources.Fox
                Dim ico As System.Drawing.Icon = System.Drawing.Icon.FromHandle(My.Resources.Fox.GetHicon())
                Me.Icon = ico
                For Each Itm As PictureBox In FlowLayoutPanel1.Controls
                    Itm.BackColor = Color.Transparent
                    PickImage()
                    Itm.Image = MyImage
                    Itm.Tag = MyTag
                Next
            Else
                Label1.Text = ("Click the red button " & (N) & " times to exit.")
                For Each Itm As PictureBox In FlowLayoutPanel1.Controls
                    Itm.BackColor = PickColor()
                    Itm.Image = Nothing
                Next
                Me.Icon = My.Resources.Icon
            End If
        End Sub
    
    End Class
    Attached Images Attached Images   
    Last edited by Amerigoware; Dec 1st, 2021 at 08:30 AM.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    I was trying to think up a good joke about your field of chickens, but they were all just too fowl.

    I'd have to say, though, that I'm intrigued as to what you are doing. You've put so many clues into those images that I keep going back and forth at guessing what the point is. I can see a couple different possibilities. Care to tell us?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    It's just a quick distraction to take a break while I'm writing a story or programming. Just click on the "red button" or the "Fox" (something you don't want in your chicken coop) 5 times to "win" and close the app. They show up far less often than the other options. If you click a button that isn't red or isn't displaying the fox, the game gets faster and more difficult.

    Obviously, anyone using this code can choose their own images with one being something that doesn't belong. Perhaps a factory among houses, or cabbage with candy. The possibilities are endless.

    The cursor I'm using looks like a gun scope crosshairs. --apparently, cursors don't show up in screenshots.

    *Just a note of interest... it unexpectedly demonstrated to me how much easier it is to see a specific color than it is to see a fox.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [vb.net 2021] [core 6.0] comparing images

    Quote Originally Posted by Amerigoware View Post
    Oof. Yeah, I have 2022 (Current). Thanks for the code. With some changes I've made, it doesn't work with changing the image of a picturebox, although it would for the backgroundimage. I'm sure it might if I insert a ctype in there.
    It would work if you used OfType in your controls loop as i showed you. You really should turn Option Strict on. That would prevent fowl language

  10. #10

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: [vb.net 2021] [core 6.0] comparing images

    Quote Originally Posted by .paul. View Post
    It would work if you used OfType in your controls loop as i showed you. You really should turn Option Strict on. That would prevent fowl language
    It is on!

    It doesn't work because I changed from buttons to pictureboxes and from backgroundimages to images. I could, I think, use backgroundimage on the picturesboxes with same result assigning images to picturebox.images.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    Quote Originally Posted by Amerigoware View Post
    It's just a quick distraction to take a break while I'm writing a story or programming. Just click on the "red button" or the "Fox" (something you don't want in your chicken coop) 5 times to "win" and close the app. They show up far less often than the other options. If you click a button that isn't red or isn't displaying the fox, the game gets faster and more difficult.

    Obviously, anyone using this code can choose their own images with one being something that doesn't belong. Perhaps a factory among houses, or cabbage with candy. The possibilities are endless.

    The cursor I'm using looks like a gun scope crosshairs. --apparently, cursors don't show up in screenshots.

    *Just a note of interest... it unexpectedly demonstrated to me how much easier it is to see a specific color than it is to see a fox.
    That's pretty cool. It's an interesting idea. You're right, too, that color is MUCH easier to see than the fox, for me. Probably not for everybody, though. I wrote a program that was pretty colorful, one time. Then I got to hear my color blind colleague trying to figure it out with my red/green colorblind boss. That was informative, too. They could use the program, but the colors I had chosen meant that they both saw the program differently than I did.

    And using Option Strict is always a good idea. It will not prevent a foxxy pun, in case you're feeling chicken, but fowl language is just birds of a different feather.
    My usual boring signature: Nothing

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    FlowLayoutPanel1.Controls.OfType(Of Any_Control_You_Choose)???

  13. #13

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    Quote Originally Posted by .paul. View Post
    FlowLayoutPanel1.Controls.OfType(Of Any_Control_You_Choose)???
    Yes, that part works just fine. The problem is assigning an image later because "'mage' is not a member of 'control'". if using picturebox.backgroundimage, it does work ...although I messed something up somehow causing all the picturebox to display the same chicken after a minute or so.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    For each pb As PictureBox in FlowLayoutPanel1.Controls.OfType(Of PictureBox)???

    Or…

    For each b As Button in FlowLayoutPanel1.Controls.OfType(Of Button)???

  15. #15

    Thread Starter
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: [RESOLVED] [vb.net 2021] [core 6.0] comparing images

    That works. I wasn't trying to be argumentative, just not understanding what you were getting at. Your help is always very much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width