Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to use IIF statement with the image property of a control? The code below will always return "DOWN" even if the image is set to accept.

state = IIf(lblModemUSB.Values.Image Is My.Resources.accept, "UP", "DOWN")


lblModemUSB.Values.Image returns a normal System.Drawing.Image
My.Resources.accept is an image in my resources called accept.png

*solution*
here is the solution i am now using.

VB
Public Function Hash_Images(ByVal image1 As Image, ByVal image2 As Image) As String
        Dim image1Stream As System.IO.MemoryStream = New MemoryStream()
        Dim image2Stream As System.IO.MemoryStream = New MemoryStream()
        image1.Save(image1Stream, System.Drawing.Imaging.ImageFormat.Bmp)
        image2.Save(image2Stream, System.Drawing.Imaging.ImageFormat.Bmp)
        Dim image1Data As Byte() = New Byte(image1Stream.Length - 1) {}
        image1Stream.Read(image1Data, 0, image1Data.Length)
        Dim image2Data As Byte() = New Byte(image2Stream.Length - 1) {}
        image2Stream.Read(image2Data, 0, image2Data.Length)
        Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim hash1 As Byte() = md5.ComputeHash(image1Data)
        Dim hash2 As Byte() = md5.ComputeHash(image2Data)
        Dim hash1String As String = System.Text.Encoding.ASCII.GetString(hash1)
        Dim hash2String As String = System.Text.Encoding.ASCII.GetString(hash2)
        If hash1String = hash2String Then
            Return "same"
        Else
            Return "different"
        End If
    End Function
Posted
Updated 1-Feb-10 14:22pm

IIf works with any expression that evaluates into a boolean. As Christian said, the test doesn't check whether the images represent the same picture but rather whether the variables refer to the same object instance[^].

You could write a Method Extension to compute the hash of the image, and base your comparison on that :)
 
Share this answer
 
I guess you have your answer. I suspect you're running into difficulties because you're hoping to test if they are the same thing, and VB is testing if they are the same instance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900