可以使用GDI直接对比2张连续图片差异吗?
不知道有哪个API 可以直接对比2张图的差异,把前一张图没有的直接从后一张图画过去, 像数对比全屏图片太慢了。 请大侠们不吝赐教 api GDI GDI+
[解决办法]
Option Explicit
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
Public Declare Function GetTickCount Lib "kernel32" () As Long
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'函数功能:判断两幅图片是否相同,这两幅图片都必须是BMP格式的
'参数说明:picOne:第一幅图片
' :picTwo:第二副图片
'返回说明:如果两幅图片相同返回:True,否则返回:False
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Public 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
On Error GoTo errFun
Rem 判断两幅图片的类型,如果不全是BMP格式,那么不比较,判为不一致
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
Rem 判断两个图片的宽和高,如果不一致,那么判为不一致
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
Exit Function
errFun:
IsTheSamePicture = False
End Function
Option Explicit
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, 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 SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Const SRCCOPY = &HCC0020
Private Type ExBmp
hDC As Long
hBP As Long
oBP As Long
End Type
Dim ABmp As ExBmp
Dim BBmp As ExBmp
Dim CBmp As ExBmp
Private Sub Command1_Click()
Call CopyDesk(ABmp.hDC)
End Sub
Private Sub Command2_Click()
Call CopyDesk(BBmp.hDC)
End Sub
Private Sub Command3_Click()
Dim Tick As Long
Tick = GetTickCount
Me.Cls
Call Contrast(ABmp.hBP, BBmp.hBP)
Call BitBlt(Me.hDC, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY, BBmp.hDC, 0, 0, SRCCOPY)
MsgBox "耗时:" & GetTickCount - Tick
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
Call CretTdc(ABmp.hDC, ABmp.hBP, ABmp.oBP, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
Call CretTdc(BBmp.hDC, BBmp.hBP, BBmp.oBP, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
Call CretTdc(CBmp.hDC, CBmp.hBP, CBmp.oBP, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call DeleTdc(ABmp.hDC, ABmp.hBP, ABmp.oBP)
Call DeleTdc(BBmp.hDC, BBmp.hBP, BBmp.oBP)
Call DeleTdc(CBmp.hDC, CBmp.hBP, CBmp.oBP)
End Sub
Private Function CretTdc(ByRef hDC As Long, ByRef hBP As Long, ByRef oBP As Long, ByVal Wdt As Long, ByVal Hgt As Long) As Long
Call DeleTdc(hDC, hBP, oBP)
CretTdc = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0&)
If CretTdc = 0 Then Exit Function
hDC = CreateCompatibleDC(CretTdc)
If hDC <> 0 Then
hBP = CreateCompatibleBitmap(CretTdc, Wdt, Hgt)
If hBP <> 0 Then
oBP = SelectObject(hDC, hBP)
If oBP <> 0 Then
Call DeleteDC(CretTdc)
Exit Function
End If
End If
End If
Call DeleTdc(hDC, hBP, oBP)
Call DeleteDC(CretTdc)
End Function
Private Sub DeleTdc(ByVal hDC As Long, ByVal hBP As Long, ByVal oBP As Long)
If oBP <> 0 Then
Call SelectObject(hDC, oBP)
oBP = 0
End If
If hBP <> 0 Then
Call DeleteObject(hBP)
hBP = 0
End If
If hDC <> 0 Then
Call DeleteDC(hDC)
hDC = 0
End If
End Sub
Private Function CopyDesk(ByVal hDC As Long) As Long
If hDC = 0 Then Exit Function
CopyDesk = GetDC(GetDesktopWindow)
Call BitBlt(hDC, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY, CopyDesk, 0, 0, SRCCOPY)
Call ReleaseDC(GetDesktopWindow, CopyDesk)
End Function
Private Sub Contrast(ByVal Sbmp As Long, ByVal Dbmp As Long)
Dim dW As Long
Dim dH As Long
Dim dL As Long
Dim Sary() As Byte
Dim Dary() As Byte
Dim vI As Long
Dim vJ As Long
Dim vK As Long
dW = Screen.Width / Screen.TwipsPerPixelX
dH = Screen.Height / Screen.TwipsPerPixelY
dL = dW * dH * 4
ReDim Sary(dL - 1)
ReDim Dary(dL - 1)
Call GetBitmapBits(Sbmp, dL, Sary(0))
Call GetBitmapBits(Dbmp, dL, Dary(0))
For vI = 0 To dW - 1
For vJ = 0 To dH - 1
vK = (vI + vJ * dW) * 4
If Sary(vK) = Dary(vK) And Sary(vK + 1) = Dary(vK + 1) And Sary(vK + 2) = Dary(vK + 2) Then
Dary(vK) = 255
Dary(vK + 1) = 255
Dary(vK + 2) = 255
End If
Next
Next
Call SetBitmapBits(Dbmp, dL, Dary(0))
End Sub