Results 1 to 9 of 9

Thread: Equals for Images?

  1. #1

    Thread Starter
    Member matt2790's Avatar
    Join Date
    Mar 2009
    Posts
    35

    Question Equals for Images?

    With this line...
    Code:
    If PictureBox1.Image = My.Resources._2star Then
    I get this error: Operator '=' is not defined for types 'System.Drawing.Image' and 'System.Drawing.Bitmap'.

    Is there another way to say = with images?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Equals for Images?

    It seems like you're using VB.NET, and not VB6 or earlier. Is this the case?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Equals for Images?

    Code:
    Public Function checkImages(bm1 As PictureBox, bm2 As PictureBox) as Boolean
        For x As Integer = 0 To bm1.Width - 1
            For y As Integer = 0 To bm1.Height - 1
                If Not bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, _
                    y)) Then
                    Return False
                End If
            Next y
        Next x
    End Function
    I think

  4. #4
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: Equals for Images?

    im pretty sure you can use safearray to convert the image to an array of integers and make some sort of checksum value to identify the picture, would seem a lot faster than comparing pixels. also you could just compare arrays as well if you wanted as well.

  5. #5
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: Equals for Images?

    here is an example of what i meant by using SafeArray. this works rather fast imo.
    to sample this you need to make a picture box and a command button. add a picture to the picturebox.

    this returns a value to identify the picture. of course you cannot guarantee every picture will have a unique value using my method. but i would think the odds of having 2 identical picture values are pretty slim.
    as i said in my earlier post, you can also use this method to compare 2 pictures by just comparing values of each array with the other. that may prove to be a better way to do what you need.

    anyway i hope this helps.


    Code:
    Option Explicit
    
    Private Type SAFEARRAYBOUND
        cElements As Long
        lLbound As Long
    End Type
    
    Private Type SAFEARRAY1D
        cDims As Integer
        fFeatures As Integer
        cbElements As Long
        cLocks As Long
        pvData As Long
        Bounds(0 To 0) As SAFEARRAYBOUND
    End Type
    
    Private Type BITMAP
        bmType As Long
        bmWidth As Long
        bmHeight As Long
        bmWidthBytes As Long
        bmPlanes As Integer
        bmBitsPixel As Integer
        bmBits As Long
    End Type
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
    Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long
    
    Private Function GetValueOfPicture(pPicBox As PictureBox) As Long
    
        Dim MyImage As BITMAP
        Dim MyArray As SAFEARRAY1D
        Dim MyPicData() As Long
        Dim MyValue As Long
        Dim i As Long
        
        Call GetObjectAPI(pPicBox.Picture, Len(MyImage), MyImage)
        With MyArray
            .cbElements = 4 'use 4 for long integer
            .cDims = 1 '1 dimensional array
            .Bounds(0).lLbound = 0
            .Bounds(0).cElements = (MyImage.bmHeight * MyImage.bmWidthBytes) / 4 'use 4 for long integer
            .pvData = MyImage.bmBits
        End With
        CopyMemory ByVal VarPtrArray(MyPicData), VarPtr(MyArray), 4
        For i = 0 To UBound(MyPicData)
            'i know my method of obtaining a value here isnt the best method. i couldnt think of a better way. :(   if you can, find a more precise method.
            MyValue = CLng((MyValue Mod &H70000000) + (MyPicData(i) Mod &HFFFFFFF))
        Next i
        CopyMemory ByVal VarPtrArray(MyPicData), 0&, 4 'clean up the array //THIS IS A MUST
    
        GetValueOfPicture = MyValue
    
    End Function
    
    Private Sub Command1_Click()
    
        MsgBox GetValueOfPicture(Picture1)
    
    End Sub

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Equals for Images?

    I answered a similar question in the recent past, but for VB6 not .Net. From what little the poster supplied, it appears images are being extracted from a resource file and added to an image control (maybe something like a memory game?). Don't know.

    If my assumptions are true, then when you extract any image from your resource, assign the .Tag property of the Image control, the resource ID you used. Then when needed to compare, you just compare the .Tag property with the resource ID. No need to physically compare images -- again, if my assumptions are correct.
    Last edited by LaVolpe; Aug 17th, 2009 at 10:53 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Equals for Images?

    Quote Originally Posted by Atheist View Post
    It seems like you're using VB.NET, and not VB6 or earlier. Is this the case?
    This error
    Quote Originally Posted by Error
    I get this error: Operator '=' is not defined for types 'System.Drawing.Image' and 'System.Drawing.Bitmap'.
    does not exist in VB6, so yes, VB.NET is being used and I have moved the thread accordingly.

    @matt2790: Because your question was originally posted in the VB6 section, I'm afraid none of the coding suggestions prior to this will work for you.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Equals for Images?

    You could presumably adapt Billy Conner's code to work in VB.NET but there's probably no need. The reason for the original error message is that the = operator is not defined for most classes. For reference types you generally have to use the Is operator:
    vb.net Code:
    1. If PictureBox1.Image Is My.Resources._2star Then
    The Is operator tests referential equality. That means that that code will tell you whether those two references refer to the same object, not whether the two objects have the same contents. The problem is that it can never be True because My.Resources._2star will return a new object every time. Even this won't be True:
    vb.net Code:
    1. If My.Resources._2star Is My.Resources._2star Then
    because each time the resource is accessed a new Image object is created.

    What you can do is just access the resource once and assign the result to a member variable:
    vb.net Code:
    1. Private _2star As Image = My.Resources._2star
    If you then use that variable all the time instead of the resource then you'll only ever create a single Image object. Assuming that you've assigned that variable to the PictureBox's Image property in the first place:
    vb.net Code:
    1. PictureBox1.Image = Me._2star
    then the following condition will evaluate to True:
    vb.net Code:
    1. If PictureBox1.Image Is Me._2star Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Equals for Images?

    Like jmcilhinney said - u can use the is operator if u want to see if the object is the resource - but if u want to compare to determine if the image is the same you will need to loop through each pixel and as soon as one is diff the image is not the same...

    There is also ways to speed this process up ...
    1. create 2 bitmaps of smaller resolution (eg 400x300)
    2. create a graphics object of these bitmaps
    3. set smoothing mode to highquality
    4. drawimage to fit onto both bitmaps - one the orig image and one the compairer
    5. check all pixels on the downsized images - as soon as one is different the images are not the same

    smoothing mode is set as that way each downsized pixel should be an average of the colors coverage of the scaled down area to give a more accurate result eg if the orig image size is 800x600 and the downsize is 400x300 there will be 4 pixels per pixel in the new image - if two of those pixels are red (255,255,0,0) and two are white (255,255,255,255) the color of the new pixel will be 255,255,127,127 - ie an average of the covered area.

    Kris

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