809
社区成员
发帖
与我相关
我的任务
分享
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
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 Const DIB_RGB_COLORS As Long = 0
Private Const OBJ_BITMAP As Long = 7
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetObjectType Lib "gdi32" (ByVal hgdiobj As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal Hdc As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal Hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal Hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal Hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function CompMemory Lib "ntdll.dll" Alias "RtlCompareMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32.dll" (ByVal Hdc As Long, ByRef pBitmapInfo As Any, ByVal un As Long, ByRef Pointer As Long, ByVal Handle As Long, ByVal Dw As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Function IsTheSamePicture(PicOne As StdPicture, PicTwo As StdPicture) As Boolean
Dim BmpOne As BITMAP, BmpTwo As BITMAP
Dim HandleOne As Long, HandleTwo As Long
Dim MemoryOne As Long, MemoryTwo As Long
Dim HdcOne As Long, HdcTwo As Long
Dim BmpInfo As BITMAPINFOHEADER
Dim ScreenDC As Long
Dim ByteChecked As Long
If GetObjectType(PicOne.Handle) <> OBJ_BITMAP Or GetObjectType(PicTwo.Handle) <> OBJ_BITMAP Then
IsTheSamePicture = False
Else
GetObject PicOne.Handle, Len(BmpOne), BmpOne
GetObject PicTwo.Handle, Len(BmpTwo), BmpTwo
If BmpOne.bmWidth <> BmpTwo.bmWidth Or BmpOne.bmHeight <> BmpTwo.bmHeight Or BmpOne.bmBitsPixel <> BmpTwo.bmBitsPixel Then
IsTheSamePicture = False
ElseIf BmpOne.bmBits <> 0 And BmpTwo.bmBits <> 0 Then
ByteChecked = CompMemory(ByVal BmpOne.bmBits, ByVal BmpTwo.bmBits, BmpOne.bmWidthBytes * BmpOne.bmHeight)
IsTheSamePicture = CBool(ByteChecked = BmpOne.bmWidthBytes * BmpOne.bmHeight)
Else
If BmpOne.bmBits <> 0 Then
MemoryOne = BmpOne.bmBits
Else
ScreenDC = GetDC(0)
HdcOne = CreateCompatibleDC(ScreenDC)
With BmpInfo
.biSize = 40
.biBitCount = 24
.biHeight = BmpOne.bmHeight
.biWidth = BmpOne.bmWidth
.biPlanes = 1
.biSizeImage = ((BmpOne.bmWidth * 3 + 3) And &HFFFFFFFC) * BmpOne.bmHeight
End With
HandleOne = CreateDIBSection(HdcOne, BmpInfo, DIB_RGB_COLORS, MemoryOne, 0, 0)
SelectObject HdcOne, HandleOne
PicOne.Render HdcOne + 0&, 0&, 0&, BmpOne.bmWidth + 0&, BmpOne.bmHeight + 0&, 0, PicOne.Height, PicOne.Width, -PicOne.Height, ByVal 0
ReleaseDC 0, ScreenDC
End If
If BmpTwo.bmBits <> 0 Then
MemoryTwo = BmpTwo.bmBits
Else
ScreenDC = GetDC(0)
HdcTwo = CreateCompatibleDC(ScreenDC)
With BmpInfo
.biSize = 40
.biBitCount = 24
.biHeight = BmpTwo.bmHeight
.biWidth = BmpTwo.bmWidth
.biPlanes = 1
.biSizeImage = ((BmpTwo.bmWidth * 3 + 3) And &HFFFFFFFC) * BmpTwo.bmHeight
End With
HandleTwo = CreateDIBSection(HdcTwo, BmpInfo, DIB_RGB_COLORS, MemoryTwo, 0, 0)
SelectObject HdcTwo, HandleTwo
PicTwo.Render HdcTwo + 0&, 0&, 0&, BmpTwo.bmWidth + 0&, BmpTwo.bmHeight + 0&, 0, PicTwo.Height, PicTwo.Width, -PicTwo.Height, ByVal 0 '类似于BMP的逆序存储,所以用-StdPic.Height
ReleaseDC 0, ScreenDC
End If
ByteChecked = CompMemory(ByVal MemoryOne, ByVal MemoryTwo, BmpInfo.biSizeImage)
IsTheSamePicture = CBool(ByteChecked = BmpInfo.biSizeImage)
If HdcOne <> 0 Then DeleteDC HdcOne
If HdcTwo <> 0 Then DeleteDC HdcTwo
If HandleOne <> 0 Then DeleteObject HandleOne
If HandleTwo <> 0 Then DeleteObject HandleTwo
End If
End If
End Function
Option Explicit
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim lngW As Long
Dim lngH As Long
Dim lngP As Long
Dim lngT As Long
Dim lngD As Long
Dim lngColor(0 To 1) As Long
On Error GoTo errSub
lngW = picP(0).ScaleWidth
lngH = picP(0).ScaleHeight
lngD = GetTickCount
For lngP = 0 To lngW
For lngT = 0 To lngH
lngColor(0) = GetPixel(picP(0).hdc, lngP, lngT)
lngColor(1) = GetPixel(picP(1).hdc, lngP, lngT)
If lngColor(0) <> lngColor(1) Then
MessageBox hwnd, "图片不一样!比较用时:" & GetTickCount - lngD & "ms", "系统提示", vbOKOnly + vbExclamation
Exit Sub
End If
Next lngT
Next lngP
MessageBox hwnd, "图片一致!比较用时:" & GetTickCount - lngD & "ms", "系统提示", vbOKOnly + vbExclamation
Exit Sub
errSub:
End Sub
Private Sub Form_Load()
Dim lngP As Long
On Error GoTo errSub
picP(0).ScaleMode = 3
picP(1).ScaleMode = 3
picP(0).Picture = LoadPicture("E:\文档资料\图片资料\G.Batistuta\1.bmp")
picP(1).Picture = LoadPicture("E:\文档资料\图片资料\G.Batistuta\1.bmp")
Exit Sub
errSub:
End Sub