(VB) Use GetBitmapBits, SetBitMapBits to accelerate image processing

zhaozj2021-02-17  77

(VB) Use GetBitmapBits, SetBitMapBits to accelerate image processing

(I don't know if there is anyone in front, it is mainly some of the experiences of recently written procedures. I hope to be useful for everyone. I haven't written something for a long time, there will be a pile of problems, will be.)

Two good stuffs, setBitmapBits, getBitmapBits, which enhance certain image processing can be greatly found.

Quote the words "SetBitmapBits: VB declaration: Declare Function setBitmapBits lib" gdi32 "(Byval dwcount as long, lpbits as a" AS Long) AS Long Parameters: Hbitmap Long, bitmap DWCount Long, want to copy the number of bytes, point to a pointer to a buffer. This buffer contains a bitmap that is properly formatted for bitmaps.

GetBitmapBits: VB Declaration: Declare Function GetBitmapbits LIB "GDI32" (Byval DWCount As long, LPBITS ANY) AS Long Role: "Copy the binary bit from bitmap to a buffer" parameter: hbitmap long, The handle of the bitmap dwcount long, who wants to copy the number of bytes. If set to zero, it means that the number of bytes in the bitmap is acquired, pointing to a pointer to a buffer of the accommodation bitmap. Note that at least the buffer is initialized into DWCOUNT byte "

For example, rotate the picture 90 degrees, below is a function of 90 degrees I wrote clockwise. Assume that the width of the target image is equal to the length of the source map, the length of the target image is equal to the width of the target image, two graph color value occupied The number of digits: HsrcBMP, the handle of the source diagram, the VB corresponds to the handle of Picture.handle HDestBMP, the target bitmap

Wherein used GetObject, CopyMemory BITMAP type and function, the following statement Declare Function GetObject Lib "gdi32" Alias ​​"GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As LongDeclare 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

'Rotate 90 degrees clockwise:

PUBLIC FUNCTION TURNBMP (HSRCBMP As Long, HDestBmp As Long) AS Booleandim x As long, y

DIM BYTESPIEL AS Long

Dim Tsbmpinfo As Bitmap, TDBMPINFO As Bitmapdim Sbits () AS BYTE, DBITS () AS BYTE

'Obtains bitmap information Call GetObject (hSrcBmp, Len (tSBmpInfo), tSBmpInfo) Call GetObject (hDestBmp, Len (tDBmpInfo), tDBmpInfo)' space applications ReDim sBits (1 To tSBmpInfo.bmWidthBytes, 1 To tSBmpInfo.bmHeight) ReDim dBits ( 1 to tDBmpInfo.bmWidthBytes, 1 to tDBmpInfo.bmHeight) 'obtained source image and the target bin FIG Call GetBitmapBits (hSrcBmp, tSBmpInfo.bmWidthBytes * tSBmpInfo.bmHeight, sBits (1, 1)) Call GetBitmapBits (hDestBmp, tDBmpInfo.bmWidthBytes * TDBmpinfo.Bmheight, DBITS (1, 1)))

'How many bytes of calculating color values ​​are occupied bytespixel = tsbmpinfo.Bmbitspixel / 8

'Rotating 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 Xnext Y

'Copy the results of the rotation to the target bitmap Call setBitMapbit (HDestBMP, TDBMPINFO.BMWIDTHBYTES * TDBMPINFO.BMHEIGHT, DBITS (1, 1))

END FUNCTION

'Call, must use image attributes, otherwise there will be problems call turnbmp (picture1.image.handle, Picture2.image.handle)

In my machine (single drain 600, win2ksp3), handle a pair of 600 * 800 pictures, running about 0.8 seconds in IDE, compiling into EXE, compile option is "Optimize for fast code". Run, <0.4 seconds

If you are interested, you can try to use setpixelv, getpixel to do something above, and will definitely slowly SETPIXELV, getPixel's approach to the VB method is Pset, Point, this is not necessary, this slow is even more powerful.

Lingll 2003-7-5

转载请注明原文地址:https://www.9cbs.com/read-31318.html

New Post(0)