|
-
Aug 18th, 2012, 06:39 AM
#1
Thread Starter
New Member
[RESOLVED] How to compare images in VBA?
Hi all
I need to compare to jpeg images and display the differences. This is what i have written but it does not work.
Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Public Function Min(ByVal d1 As Integer, ByVal d2 As Integer) As Integer
Min = d1
If d2 < d1 Then Min = d2
End Function
Private Sub CommandButtonGenerate_Click()
' Load the images.
Dim RefPath As String
Dim StudPath As String
StudPath = CommonDialogDrawStudent.FileName
RefPath = CommonDialogDraw.FileName
ImageStud.Picture = LoadPicture(StudPath)
ImageRef.Picture = LoadPicture(RefPath)
' Make a difference image.
Dim wid As Integer
Dim hgt As Integer
wid = Min(ImageStud.Picture.Width, ImageRef.Picture.Width)
hgt = Min(ImageStud.Picture.Height, ImageRef.Picture.Height)
Dim x As Integer
Dim y As Integer
Dim ColourStud As Long
Dim ColourRef As Long
Dim Resultdiff As Long 'this is where the error comes in, but i couldnt figure it out
ImageDiff.Picture.Width = wid
ImageDiff.Picture.Height = hgt
ColourStud = ImageStud.Picture
ColourRef = ImageRef.Picture
Resultdiff = ImageDiff.Picture
For x = 0 To wid - 1
For y = 0 To hgt - 1
If GetPixel(ColourStud, x, y) = GetPixel(ColourRef, x, y) Then
SetPixel Resultdiff, x, y, &H0
Else
SetPixel Resultdiff, x, y, &HFF0000
End If
Next y
Next x
End Sub
-
Aug 19th, 2012, 02:48 AM
#2
Re: How to compare images in VBA?
'this is where the error comes in, but i couldnt figure it out
what error?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 19th, 2012, 03:19 AM
#3
Thread Starter
New Member
Re: How to compare images in VBA?
When the program runs to this line "ImageDiff.Picture.Width = wid"
The debugger will show this error message "Run-time error '91' Object Variable or With block varaible not set"
-
Aug 19th, 2012, 06:57 AM
#4
Re: How to compare images in VBA?
"Run-time error '91' Object Variable or With block varaible not set"
indicates no picture
the width of the picture maybe read only
you can resize the image control, then use the picturesizemode to the size of the image control
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 20th, 2012, 08:20 AM
#5
Thread Starter
New Member
Re: How to compare images in VBA?
Hi
I tired to use a frame (called Frame1) instead of a image to store the differences. This time round, i get the errror "Run-time error '438'. Object does not supprot this method or property" when it runs to this line "SetPixel Frame1.hDC, x, y, &H0". How to use GetPixel, SetPixel? Thanks.
Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Public Function Min(ByVal d1 As Integer, ByVal d2 As Integer) As Integer
Min = d1
If d2 < d1 Then Min = d2
End Function
Private Sub CommandButtonGenerate_Click()
' Load the images.
Dim RefPath As String
Dim StudPath As String
StudPath = CommonDialogDrawStudent.FileName
RefPath = CommonDialogDraw.FileName
ImageStud.Picture = LoadPicture(StudPath)
ImageRef.Picture = LoadPicture(RefPath)
' Make a difference image.
Dim wid As Integer
Dim hgt As Integer
wid = Min(ImageStud.Picture.Width, ImageRef.Picture.Width)
hgt = Min(ImageStud.Picture.Height, ImageRef.Picture.Height)
Dim x As Integer
Dim y As Integer
Dim ColourStud As Long
Dim ColourRef As Long
Frame1.Width = wid
Frame1.Height = hgt
ColourStud = ImageStud.Picture
ColourRef = ImageRef.Picture
For x = 0 To wid - 1
For y = 0 To hgt - 1
If GetPixel(ColourStud, x, y) = GetPixel(ColourRef, x, y) Then
SetPixel Frame1.hDC, x, y, &H0 ' here is the error
Else
SetPixel Frame1.hDC, x, y, &HFF0000
End If
Next y
Next x
End Sub
-
Aug 20th, 2012, 04:26 PM
#6
Re: How to compare images in VBA?
frames are lightweight controls and do not have hdc, in fact i am not sure that any forms2 controls have hdc
you might have to try things like getDC API
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 22nd, 2012, 06:00 AM
#7
Thread Starter
New Member
Re: How to compare images in VBA?
Hi
Would you elaborate on how to use getDC. Thank you.
-
Aug 23rd, 2012, 04:59 AM
#8
Re: How to compare images in VBA?
search in this forum, there have been examples by koolsid and others
if the search is still broken use google
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
|