检查图片框是否包含图像

时间:2014-01-19 10:21:09

标签: visual-studio vb6 comparison picturebox

是否有PictureBox组件的内置方法来检查其内容的某个部分是否与其他PictureBox的内容相匹配? e.g:

enter image description here

我知道我可以通过将每个像素的颜色与PictureBox's Point(X,Y)方法进行比较来实现,但这对我来说似乎有些过分。更不用说它可能太慢了。

1 个答案:

答案 0 :(得分:0)

您可以通过访问图片的内存来更快地比较每个像素 它非常简单快速,当我是一名新的VB6程序员时,我用这种方式编写了一个2D游戏引擎。 My 2D Game Engine (yLib)

你需要做的就是得到第一个像素的地址 以下功能为您完成工作:

模块1:


    Public Declare Function VarPtrArray Lib "msvbvm60" Alias "VarPtr" (Ptr() As Any) As Long
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    Public Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (destination As Any, ByVal Length As Long)

    Public Type SAFEARRAYBOUND
        cElements As Long
        lLbound As Long
    End Type
    Public Type SAFEARRAY2D
        cDims As Integer
        fFeatures As Integer
        cbElements As Long
        cLocks As Long
        pvData As Long
        Bounds(0 To 1) As SAFEARRAYBOUND
    End Type
    Public 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
    Public Type Picture
        pic As STDPicture
        c() As Byte
        bmp As BITMAP
        SA As SAFEARRAY2D
    End Type

    Public Type lyPicture
        pic As STDPicture
        c() As Byte
        bmp As BITMAP
        SA As SAFEARRAY2D
    End Type

主要表单或模块:


    Private Sub LoadPicArray2D(val As lyPicture)
        ' get bitmap info from image box
        Call GetObjectAPI(val.pic, Len(val.bmp), val.bmp)  'dest
        ' exit if not 24-bit bitmap
        ' make the local matrix point to bitmap pixels
        If val.bmp.bmPlanes  1 Or val.bmp.bmBitsPixel  24 Or val.bmp.bmWidthBytes / val.bmp.bmWidth  3 Then
            Call Err.Raise(500, "Only 24-bit bitmaps.", "Only 24-bit bitmaps.")
        End If
        With val.SA
          .cbElements = 1
          .cDims = 2
          .Bounds(0).lLbound = 0
          .Bounds(0).cElements = val.bmp.bmHeight
          .Bounds(1).lLbound = 0
          .Bounds(1).cElements = val.bmp.bmWidthBytes
          .pvData = val.bmp.bmBits
        End With
        Call CopyMemory(ByVal VarPtrArray(val.c()), VarPtr(val.SA), 4)
    End Sub

    Public Sub Main()
        Dim pic As lyPicture
        Set pic.pic = Form1.TargetPictureBox.Picture
        Call LoadPicArray2D(pic)

        'Just remember that the picture's memory stored top-down this mean the 
        ' upper pixel lays in lower array bound.
        ' but the array is correct in horizontal arrangement.
        ' And the array is formated BGR pic.c(0,0) = B , pic.c(1,0) = G, pic.c(2,0) = R

        pic.c(0,0) = 0                      ' the pixel at the left bottom (0, MAX_PICTURE_HEIGHT-1) of the picture.
        pic.c(0,MAX_PICTURE_HEIGHT-1) = 0     ' the pixel at (0, 0)

        ' This is very IMPORTANT to release the array at the end of the code.
        Call ZeroMemory(ByVal VarPtrArray(val.c()), 4)
        ' I dont tested this code just copied from my game engine. :D
    End Sub