2 Replies - 8543 Views - Last Post: 14 December 2012 - 04:56 AM Rate Topic: -----

#1 Runos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-December 12

Using an IF Statement to check Image in Picture Box

Posted 13 December 2012 - 07:52 PM

What I want to do is check a picture box. If it is one image, then I want it to do one action. If it is any other image, do a different action. I'm checking for an image located at My.Resources."image.png".

This is more or less what I want to accomplish (in pseudo code):


If PictureBox1 = (xxxx.png) Then

MsgBox(True)

Else

MsgBox(False)

End If




I have tried lots of things, these two were the ones I thought had the best chance of working:


If PictureBox1.Image = My.Resources."image.png" Then


This one spits out an error: (Operator '=' is not defined for types 'System.Drawing.Image' and 'System.Drawing.Bitmap')




If PictureBox1.Image.Equals(My.Resources."image.png") Then


This one just goes straight to the "Else" action, in other words, it doesn't recognise the image as the correct one, even when it is.



Thanks in advance,
Runos

Is This A Good Question/Topic? 0
  • +

Replies To: Using an IF Statement to check Image in Picture Box

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Using an IF Statement to check Image in Picture Box

Posted 13 December 2012 - 10:06 PM

View PostRunos, on 13 December 2012 - 08:52 PM, said:

What I want to do is check a picture box. If it is one image, then I want it to do one action. If it is any other image, do a different action. I'm checking for an image located at My.Resources."image.png".
This is more or less what I want to accomplish (in pseudo code):
If PictureBox1 = (xxxx.png) Then


What you are running into is that every time you reference My.Resources.image (Note: not image.png), you create a new instance of the image. Even though they look like they should be the same thing, they aren't, because you are comparing the instance in the PictureBox with the instance created for the comparison, and it isn't comparing the bitmap itself, just the reference to a copy of it.

The solution is to create instances of the My.Resources images, assigned to variables. Then comparing the PictureBox.Image with the variable, will work. As well, the "=" doesn't do it. You need "Is".

Have a look at this:
Option Strict On
Option Explicit On

Public Class Form1
    Dim pbImage1 As Image = My.Resources.Swamp 'imported from a file called Swamp.jpg
    Dim pbimage2 As Image = My.Resources.Dusty 'Imported from a file called Dusty.jpg

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        PictureBox1.Image = pbImage1
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If PictureBox1.Image Is pbImage1 Then
            PictureBox1.Image = pbimage2
        Else
            PictureBox1.Image = pbImage1
        End If
    End Sub
End Class



Edit: Typos

This post has been edited by lar3ry: 13 December 2012 - 10:07 PM

Was This Post Helpful? 3
  • +
  • -

#3 Runos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-December 12

Re: Using an IF Statement to check Image in Picture Box

Posted 14 December 2012 - 04:56 AM

Perfect, thanks so much, your a life saver. Is there a way to give +rep or whatever on here (Im kinda new here :P)???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1