(VB) uses getbitmapbits and setbitmapbits to accelerate Image Processing

Source: Internet
Author: User

(VB) uses getbitmapbits and setbitmapbits to accelerate Image Processing

(I don't know if anyone has ever written this article. I hope it will be useful to you. If you haven't written it for a long time, there will be a bunch of problems .)

Recently, we found two good things, setbitmapbits and getbitmapbits, which can greatly improve the processing speed of some images.

Quote others' words
"Setbitmapbits:
VB Declaration: declare function setbitmapbits lib "GDI32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long
Purpose: "copy the binary bit from the buffer to a bitmap"
Parameter: hbitmap long, the bitmap handle
Dwcount long, number of bytes to be copied
Lpbits any, a pointer to a buffer. This buffer contains the bitmap bit correctly formatted for the bitmap.

Getbitmapbits:
VB Declaration: declare function getbitmapbits lib "GDI32" (byval hbitmap as long, byval dwcount as long, lpbits as any) as long
Purpose: "copy the binary bit from the bitmap to a buffer zone"
Parameter: hbitmap long, the bitmap handle
Dwcount long: the number of bytes to be copied. If it is set to zero, the number of bytes in the bitmap is obtained.
Lpbits any, a pointer to a buffer that holds bitmap bits. Note that the buffer should be initialized to at least dwcount bytes in advance"

For example, rotate an image 90 degrees. Below is a function that I wrote to rotate the image 90 degrees clockwise.
Assume that the width of the target image is equal to the length of the source image, and the length of the target image is equal to the width of the source image. The color values of the two images occupy the same number of digits.
Parameter: hsrcbmp, the source image bitmap handle, which corresponds to picture. Handle in VB
Hdestbmp: the handle of the target bitmap.

The GetObject, copymemory function, and bitmap types used are declared as follows:
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

'Functions that Rotate 90 degrees clockwise:

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

'Obtain bitmap Information
Call GetObject (hsrcbmp, Len (tsbmpinfo), tsbmpinfo)
Call GetObject (hdestbmp, Len (tdbmpinfo), tdbmpinfo)
'Application Space
Redim sbits (1 to tsbmpinfo. bmwidthbytes, 1 to tsbmpinfo. bmheight)
Redim dBits (1 to tdbmpinfo. bmwidthbytes, 1 to tdbmpinfo. bmheight)

'Obtain the binary digits of the source and target graphs.
Call getbitmapbits (hsrcbmp, tsbmpinfo. bmwidthbytes * tsbmpinfo. bmheight, sbits (1, 1 ))
Call getbitmapbits (hdestbmp, tdbmpinfo. bmwidthbytes * tdbmpinfo. bmheight, dBits (1, 1 ))

'Calculates the number of bytes occupied by the color value.
Bytespixel = tsbmpinfo. bmbitspixel/8

'Rotate
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

'Copy the rotating result to the target bitmap.
Call setbitmapbits (hdestbmp, tdbmpinfo. bmwidthbytes * tdbmpinfo. bmheight, dBits (1, 1 ))

End Function

'Call. You must use the image attribute. Otherwise there will be problems.
Call turnbmp (picture1.image. Handle, picture2.image. Handle)

On my machine (duolong 600, win2ksp3), process a 600*800 image,
It takes about 0.8 seconds to run in IDE,
Compile to EXE, and the compilation option is "optimize for fast code". Run, <0.4 seconds

If you are interested, try setpixelv and getpixel to do the above, which will be much slower.
Setpixelv. The VB method corresponding to getpixel is pset and point. There is no need to try it. This is much slower.

Lingll
2003-7-5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.