VB 如何获取屏幕图片数据保存在一个二进制数组里面?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB 如何获取屏幕图片数据保存在一个二进制数组里面?相关的知识,希望对你有一定的参考价值。
VB程序中我会用BITBLT把flash动画抓到内存中,可是如何把这个图片数据保存在一个二进制数组里面?中间不要经过什么图片等控件,直接根据桌面窗口的设备句柄和位图句柄 存储这个二进制数据(Byte数组),要和Open语句读取的数据一样,其中不包括文件头信息和图片数据,该怎么实现?即只把RGB信息存入数组即可。我不想先生成BMP图片文件以后再用Open语句读取,请高手不吝赐教!
数组中只存放颜色数据,不需要别的数据,如文件头就不要。我再把颜色数据转换成别的数据,不知道速度能发跟上。每抓一张就转换一张。请高手先讲讲思路,在写源码,最好来点注释,分不够我可以再给。
既然已经获取了窗口的设备句柄 hdc
那就 由 CreateDIBSection 生成一个位图对象
用 BitBlt 将窗口图象复制到位图对象中
CreateDIBSection 生成的位图对象的数据就可以放到 Byte数组中了
Private Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
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 BI_RGB As Long = &H0&
Private Const DIB_RGB_COLORS = 0
Private Const DIB_PAL_COLORS = 1
Private Const ERRORAPI = 0
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFOHEADER, ByVal un As Long, lplpVoid As Long, ByVal Handle As Long, ByVal dw As Long) As Long
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 CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Src As Any, ByVal cb As Long) As Long
Private Declare Sub ZeroMemory Lib "kernel32" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Private Declare Function ObjPtr Lib "msvbvm60.dll" Alias "VarPtr" (Var As Object) As Long
Private Declare Function VarPtr Lib "msvbvm60.dll" (Var As Any) As Long
Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Var() As Any) As Long
Private m_MyCreated As Boolean
Private m_MyDibInfo As BITMAPINFOHEADER
Private m_Myhdc As Long
Private m_Myhbitmap As Long
Private m_MyhbitmapOld As Long
Private m_MyBuffer As Long
Private m_MyptrByte As Long
Private m_MyWidth As Long
Private m_MyHeight As Long
Private m_MyLineAdd As Long
Private m_MyPixelAdd As Long
Private m_MyLineByteWidth As Long
Private m_pvDataPtrAdd As Long
Private m_InitPtrFlag As Boolean
Private pByte0(0 To 0) As Byte
Private pByte0Ptr(0 To 0) As Long
Private OldpByte0 As Long
Private OldpByte0Ptr As Long
Private pByte1(0 To 0) As Byte
Private pByte1Ptr(0 To 0) As Long
Private OldpByte1 As Long
Private OldpByte1Ptr As Long
Private p3Dest(0 To 2) As Byte
Private p3ByteDest(0 To 0) As Long
Private Oldp3ByteDest As Long
Private Oldp3ByteDestPtr As Long
Private p3Src(0 To 2) As Byte
Private p3ByteSrc(0 To 0) As Long
Private Oldp3ByteSrc As Long
Private Oldp3ByteSrcPtr As Long
Private pLongAll(0 To 0) As Long
Private pLongAllPtr(0 To 0) As Long
Private OldpLongAll As Long
Private OldpLongAllPtr As Long
Private Function CreateDIB(ByVal Width As Long, ByVal Height As Long, Optional ByVal iBitCount As Integer = 24) As Boolean
If m_MyCreated And m_MyWidth = Width And m_MyHeight = Height And m_MyDibInfo.biBitCount = iBitCount Then
CreateDIB = True
Exit Function
End If
DestoryDIB
If iBitCount <> 24 And iBitCount <> 32 Then
CreateDIB = False
Exit Function
End If
m_MyWidth = Width
m_MyHeight = Height
If m_MyWidth < 1 Then m_MyWidth = 1
If m_MyHeight < 1 Then m_MyHeight = 1
m_Myhdc = CreateCompatibleDC(0)
If m_Myhdc = 0 Then
m_MyWidth = 0
m_MyHeight = 0
CreateDIB = False
Exit Function
End If
With m_MyDibInfo
.biSize = Len(m_MyDibInfo)
.biWidth = m_MyWidth
.biHeight = m_MyHeight
.biPlanes = 1
.biBitCount = iBitCount
.biCompression = BI_RGB
.biClrImportant = 0
.biXPelsPerMeter = 0
.biYPelsPerMeter = 0
End With
m_Myhbitmap = CreateDIBSection(m_Myhdc, m_MyDibInfo, 0, m_MyBuffer, 0, 0)
If m_Myhbitmap = 0 Then
DeleteDC m_Myhdc
m_Myhdc = 0
m_MyWidth = 0
m_MyHeight = 0
CreateDIB = False
Exit Function
End If
m_MyhbitmapOld = SelectObject(m_Myhdc, m_Myhbitmap)
m_MyptrByte = m_MyBuffer
Dim dpixeladd As Long
dpixeladd = iBitCount \ 8
m_MyPixelAdd = dpixeladd - 3
m_MyLineByteWidth = m_MyWidth * (dpixeladd)
If (m_MyLineByteWidth Mod 4) <> 0 Then m_MyLineByteWidth = m_MyLineByteWidth + (4 - (m_MyLineByteWidth Mod 4))
m_MyCreated = True
CreateDIB = True
End Function
Private Sub DestoryDIB()
If m_MyCreated Then
DeleteObject (SelectObject(m_Myhdc, m_MyhbitmapOld))
DeleteDC m_Myhdc
m_Myhdc = 0
m_Myhbitmap = 0
m_MyhbitmapOld = 0
m_MyBuffer = 0
m_MyptrByte = 0
m_MyCreated = False
End If
End Sub
CreateDIB 就生成了一个指定宽度,高度和颜色为数的位图对象
位图句柄 m_Myhbitmap
设备描述句柄 m_Myhdc
数据区首地址 m_MyBuffer
生成位图对象后,就可以通过BitBlt做需要的处理
要取数时
ReDim bbuffer(0 To m_MyLineByteWidth * m_MyHeight) As Byte
CopyMemory bbuffer(0), ByVal m_MyBuffer, m_MyLineByteWidth * m_MyHeight
就可以把位图数据放到Byte数组中了
如果希望效率更高的话就借助指针直接读写数据必再复制到 Byte数组中
Private Sub PointInit()
If m_InitPtrFlag Then Exit Sub
MakePoint VarPtrArray(pLongAll), VarPtrArray(pLongAllPtr), OldpLongAll, OldpLongAllPtr
m_InitPtrFlag = True
MakePoint VarPtrArray(p3Dest), VarPtrArray(p3ByteDest), Oldp3ByteDest, Oldp3ByteDestPtr
MakePoint VarPtrArray(p3Src), VarPtrArray(p3ByteSrc), Oldp3ByteSrc, Oldp3ByteSrcPtr
End Sub
Private Sub PointFree()
If m_InitPtrFlag = False Then Exit Sub
FreePoint VarPtrArray(p3Dest), VarPtrArray(p3ByteDest), Oldp3ByteDest, Oldp3ByteDestPtr
FreePoint VarPtrArray(p3Src), VarPtrArray(p3ByteSrc), Oldp3ByteSrc, Oldp3ByteSrcPtr
m_InitPtrFlag = False
FreePoint VarPtrArray(pLongAll), VarPtrArray(pLongAllPtr), OldpLongAll, OldpLongAllPtr
End Sub
Private Sub MakePoint(ByVal DataArrPtr As Long, ByVal pDataArrPtr As Long, _
ByRef OldArrPtr As Long, ByRef OldpArrPtr As Long)
Dim TempLng As Long
Dim TempPtr As Long
If m_InitPtrFlag Then
pLongAllPtr(0) = DataArrPtr
TempLng = pLongAll(0) + m_pvDataPtrAdd
pLongAllPtr(0) = pDataArrPtr
TempPtr = pLongAll(0) + m_pvDataPtrAdd
pLongAllPtr(0) = TempPtr
OldpArrPtr = pLongAll(0)
pLongAll(0) = TempLng
pLongAllPtr(0) = TempLng
OldArrPtr = pLongAll(0)
Else
CopyMemory TempLng, ByVal DataArrPtr, 4
TempLng = TempLng + m_pvDataPtrAdd
CopyMemory TempPtr, ByVal pDataArrPtr, 4
TempPtr = TempPtr + m_pvDataPtrAdd
CopyMemory OldpArrPtr, ByVal TempPtr, 4
CopyMemory ByVal TempPtr, TempLng, 4
CopyMemory OldArrPtr, ByVal TempLng, 4
End If
End Sub
Private Sub FreePoint(ByVal DataArrPtr As Long, ByVal pDataArrPtr As Long, ByVal OldArrPtr As Long, ByVal OldpArrPtr As Long)
Dim TempPtr As Long
If m_InitPtrFlag Then
pLongAllPtr(0) = DataArrPtr
pLongAllPtr(0) = pLongAll(0) + m_pvDataPtrAdd
pLongAll(0) = OldArrPtr
pLongAllPtr(0) = pDataArrPtr
pLongAllPtr(0) = pLongAll(0) + m_pvDataPtrAdd
pLongAll(0) = OldpArrPtr
Else
CopyMemory TempPtr, ByVal DataArrPtr, 4
CopyMemory ByVal (TempPtr + m_pvDataPtrAdd), OldArrPtr, 4
CopyMemory TempPtr, ByVal pDataArrPtr, 4
CopyMemory ByVal (TempPtr + m_pvDataPtrAdd), OldpArrPtr, 4
End If
End Sub
Private Sub Form_Load()
m_pvDataPtrAdd = 12&
m_InitPtrFlag = False
PointInit
End Sub
Private Sub Form_Unload(Cancel As Integer)
PointFree
End Sub
Private Sub Command1_Click()
p3ByteDest(0) = m_MyBuffer
p3ByteSrc(0) = m_MyBuffer
'可以通过 p3Dest(0),p3Dest(1),p3Dest(2),p3Src(0).p3Src(1),p3Src(2)直接读写位图数据
p3Dest(0) = p3Src(0)
p3ByteDest(0) = p3ByteDest(0) + 1
p3ByteSrc(0) = p3ByteSrc(0) + 1
End Sub 参考技术A 存储在数组中,没实现过;存储到文件还是可以的
以上是关于VB 如何获取屏幕图片数据保存在一个二进制数组里面?的主要内容,如果未能解决你的问题,请参考以下文章
pgmagick,pil不保存图片并且获取图片二进制数据记录