Visual studio 检查picturebox是否包含图像
是否有任何内置的Visual studio 检查picturebox是否包含图像,visual-studio,vb6,comparison,picturebox,Visual Studio,Vb6,Comparison,Picturebox,是否有任何内置的PictureBoxCompocontent方法来检查其内容的某一部分是否与另一PictureBox的内容匹配?e、 g: 我知道我可以通过将每个像素的颜色与PictureBox的Point(X,Y)方法进行比较来做到这一点,但这对我来说似乎有点过分了。更不用说它可能太慢了 通过访问图片的内存,您可以更快地比较每个像素。 这是非常简单和快速,我写了一个2D游戏引擎使用这种方式,当我是一个新的VB6程序员 您所要做的就是获取第一个像素的地址。 以下功能为您完成此工作: 模块1:
PictureBox
Compocontent方法来检查其内容的某一部分是否与另一PictureBox
的内容匹配?e、 g:
我知道我可以通过将每个像素的颜色与
PictureBox的Point(X,Y)
方法进行比较来做到这一点,但这对我来说似乎有点过分了。更不用说它可能太慢了 通过访问图片的内存,您可以更快地比较每个像素。这是非常简单和快速,我写了一个2D游戏引擎使用这种方式,当我是一个新的VB6程序员
您所要做的就是获取第一个像素的地址。
以下功能为您完成此工作:
模块1: 主要形式或模块:
最好创建一个具有长(32位)值的SAFEARRAY,并将该SAFEARRAY结构中的pvData指向lyPictures的SAFEARRAY。使用此新数组的pvData将提供数据对齐,以混合访问对齐指针的CPU优化,然后您可以屏蔽B(颜色和&HFF)、G(颜色和&HFF00/&H100)、B(颜色和&HFF0000/&H10000)请注意,这仅在图片为32位时有效(答案中的图片必须为24位,否则会引发错误)
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