首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么快速判断两个picture是否相同

2012-01-23 
如何快速判断两个picture是否相同?如果有两个picturebox,如何判断两个box中的picture是否相同?是否可以找

如何快速判断两个picture是否相同?
如果有两个picturebox,如何判断两个box中的picture是否相同?
是否可以找出两个box的picture内存段,直接进行比较?

[解决办法]

VB code
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 LongEnd TypePrivate Type BITMAP    bmType       As Long    bmWidth      As Long    bmHeight     As Long    bmWidthBytes As Long    bmPlanes     As Integer    bmBitsPixel  As Integer    bmBits       As LongEnd TypePrivate Const DIB_RGB_COLORS      As Long = 0Private Const OBJ_BITMAP          As Long = 7Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As LongPrivate Declare Function GetObjectType Lib "gdi32" (ByVal hgdiobj As Long) As LongPrivate Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As LongPrivate Declare Function CreateCompatibleDC Lib "gdi32" (ByVal Hdc As Long) As LongPrivate Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal Hdc As Long) As LongPrivate Declare Function DeleteDC Lib "gdi32" (ByVal Hdc As Long) As LongPrivate Declare Function SelectObject Lib "gdi32" (ByVal Hdc As Long, ByVal hObject As Long) As LongPrivate Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPrivate Declare Function CompMemory Lib "ntdll.dll" Alias "RtlCompareMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As LongPrivate 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 LongPrivate Declare Function GetTickCount Lib "kernel32" () As LongPrivate 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 IfEnd Function 

热点排行