|
-
Aug 17th, 2009, 04:45 PM
#1
Thread Starter
Member
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?
-
Aug 17th, 2009, 05:09 PM
#2
Re: Equals for Images?
It seems like you're using VB.NET, and not VB6 or earlier. Is this the case?
-
Aug 17th, 2009, 05:15 PM
#3
Frenzied Member
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
-
Aug 17th, 2009, 07:37 PM
#4
Hyperactive Member
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.
-
Aug 17th, 2009, 10:10 PM
#5
Hyperactive Member
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
-
Aug 17th, 2009, 10:49 PM
#6
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.
-
Aug 18th, 2009, 05:53 AM
#7
Re: Equals for Images?
 Originally Posted by Atheist
It seems like you're using VB.NET, and not VB6 or earlier. Is this the case?
This error
 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.
-
Aug 18th, 2009, 08:45 PM
#8
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:
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:
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:
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:
PictureBox1.Image = Me._2star
then the following condition will evaluate to True:
vb.net Code:
If PictureBox1.Image Is Me._2star Then
-
Aug 18th, 2009, 11:43 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|