CSDN博客

img lingll

(vb)利用GetBitmapBits,SetBitmapBits加速图像处理

发表于2003/7/7 8:27:00  3811人阅读

分类: 编程心得

(vb)利用GetBitmapBits,SetBitmapBits加速图像处理

(不知道前面有没有人写过这个,这里主要是最近写程序的一些心得,希望对大家有用.久未写东西,会有一堆的毛病,将就着吧.)

最近发现了两个好东东,SetBitmapBits,GetBitmapBits,能极大的提高某些图像处理的速度.

引用别人的话
"SetBitmapBits:
 vb声明: Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
 作用: "将来自缓冲区的二进制位复制到一幅位图"
 参数: hBitmap Long,位图的句柄
  dwCount Long,欲复制的字节数量
  lpBits Any,指向一个缓冲区的指针。这个缓冲区包含了为位图正确格式化的位图位


GetBitmapBits:
 vb声明: Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
        作用: "将来自位图的二进制位复制到一个缓冲区"
 参数: hBitmap Long,位图的句柄
  dwCount Long,欲复制的字节数。如设为零,表示取得位图中的字节数
  lpBits Any,指向容纳位图位的一个缓冲区的指针。注意事先将缓冲区至少初始化成dwCount个字节 "


举一个例子,将图片旋转90度,下面是我写的顺时针旋转90度的函数.
假设目标图像的宽等于源图的长,目标图像的长等于源图的宽,两图颜色值占用的位数相等.
参数: hSrcBmp,源图位图的句柄,vb中对应的是Picture.Handle
 hDestBmp,目标位图的句柄

其中用到的GetObject,CopyMemory函数与BITMAP类型,声明如下
Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)
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


'顺时针旋转90度的函数:

Public Function TurnBmp(hSrcBmp As Long, hDestBmp As Long) As Boolean
Dim X  As Long, Y As Long

Dim BytesPixel As Long


Dim tSBmpInfo As BITMAP, tDBmpInfo As BITMAP
Dim sBits() As Byte, dBits() As Byte

'获得位图信息
Call GetObject(hSrcBmp, Len(tSBmpInfo), tSBmpInfo)
Call GetObject(hDestBmp, Len(tDBmpInfo), tDBmpInfo)
'申请空间
ReDim sBits(1 To tSBmpInfo.bmWidthBytes, 1 To tSBmpInfo.bmHeight)
ReDim dBits(1 To tDBmpInfo.bmWidthBytes, 1 To tDBmpInfo.bmHeight)

'获得源图与目标图二进制位
Call GetBitmapBits(hSrcBmp, tSBmpInfo.bmWidthBytes * tSBmpInfo.bmHeight, sBits(1, 1))
Call GetBitmapBits(hDestBmp, tDBmpInfo.bmWidthBytes * tDBmpInfo.bmHeight, dBits(1, 1))

'计算颜色值占用多少字节
BytesPixel = tSBmpInfo.bmBitsPixel / 8

'旋转
For Y = 1 To tSBmpInfo.bmHeight
    For X = 1 To tSBmpInfo.bmWidth
        Call CopyMemory(dBits((tSBmpInfo.bmHeight - Y) * BytesPixel + 1, X), sBits((X - 1) * BytesPixel + 1, Y), BytesPixel)
    Next X
Next Y

'将旋转的结果复制到目标位图
Call SetBitmapBits(hDestBmp, tDBmpInfo.bmWidthBytes * tDBmpInfo.bmHeight, dBits(1, 1))

End Function


'调用,一定要用image属性,不然会有问题
Call TurnBmp(Picture1.Image.handle, Picture2.Image.handle)

在我的机上(独龙600,win2ksp3),处理一副600*800的图片,
在ide中运行约0.8秒,
编译成exe,编译选项是"Optimize for Fast Code".运行,<0.4秒

有兴趣的可以试试用SetPixelV,GetPixel做上面的事情,肯定会慢许多
SetPixelV,GetPixel对应的vb的方法是pset,point,这个就没必要试了,这个慢得更厉害

                                                                                           lingll
                                                                                           2003-7-5

0 0

相关博文

我的热门文章

img
取 消
img