[Learning diary] acquisition and output of pixels in VB Image Processing

Source: Internet
Author: User
Pixel acquisition and output in VB Image Processing
Author: Region nameSource: InterNetTime added: 2005-1-28
To process an image, first obtain the pixel value of the image. Although the PICTURE control provided by VB can open many types of images, however, the POINT method provided by it is too slow to read pixels. The GetPixel API can be used quickly, because the PIONT method itself is a package for GetPixel.

In VB, the DIB method is used to quickly obtain an image opened in PICTURE, and the DDB method is also used, however, the DDB method also needs to take into account the processing of images with different color depths separately. The implementation of the program is relatively complex, and the DIB method is not required, in addition, the processing speed is slower than that of the DDB method.

Process 1: obtain all pixels of an image opened in the PICTURE control.

Public Sub DibGet (ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long)
Dim iBitmap As Long
Dim iDC As Long
Dim I As LongDim
Dim W As Long
Dim H As Long

On Error GoTo ErrLine
Done = False
TimeGet = timeGetTime
InPutWid = XEnd-XBegin
InPutHei = YEnd-YBegin
W = InPutWid + 1
H = InPutHei + 1

I = (Bits \ 8)-1
ReDim ColVal (I, InPutWid, InPutHei)
With bi24BitInfo. bmiHeader
. BiBitCount = Bits
. BiCompression = 0 &
. BiPlanes = 1
. BiSize = Len (bi24BitInfo. bmiHeader)
. BiWidth = W
. BiHeight = H
End

IBitmap = GetCurrentObject (IdSource, 7 &)
GetDIBits IdSource, iBitmap, 0 &, H, ColVal (0, 0, 0), bi24BitInfo, 0 & DeleteObject iBitmap
Done = True
TimeGet = timeGetTime-TimeGetExit Sub
ErrLine:
MsgBox "error code:" & Err. Number & ":" & Err. Description
End Sub

In this process, only some parameter settings and API calls are used, and algorithms are not involved.

Process 2: Process of image output:

Public Sub DIBPut (ByVal IdDestination As Long)
Dim W As Long
Dim H As Long

On Error GoTo ErrLine
Done = False
TimePut = timeGetTime

W = OutPutWid + 1
H = OutPutHei + 1

With bi24BitInfo. bmiHeader
. BiWidth = W
. BiHeight = H
LineBytes = (W * Bits + 31) And & HFFFFFFE0) \ 8
. BiSizeImage = LineBytes * H
End
SetDIBitsToDevice IdDestination, 0, 0, W, H, 0, 0, 0, H, ColOut (0, 0, 0), bi24BitInfo. bmiHeader, 0

Done = True
TimePut = timeGetTime-TimePut
Exit Sub
ErrLine:
MsgBox Err. Description
End Sub

The following describes the global variables, data structures, and API definitions in the process.

API definition:

Delete A DC

Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long

Delete an object

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Select current object

Private Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long

Obtain DIB

Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitMapInfo, byVal wUsage As Long) As Long

Obtain system time

Private Declare Function timeGetTime Lib "winmm. dll" () As Long

Data structure definition:

Private Type BitMapInfoHeader 'file information header -- 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 RGBQuad
RgbBlue As Byte
RgbGreen As Byte
RgbRed As Byte
'Rgbreserved As Byte
End Type

Private Type BitMapInfo
BmiHeader As BitMapInfoHeader
BmiColors As RGBQuad
End Type

These three data structures are indispensable in DIB. We don't have to go into it, but simply copy and paste them in order.

Global variables used in the process:

Private Const Bits As Long = 32' color depth. Here, all images are processed in 32 Bits.
Public Done As Boolean 'is used to mark whether a process ends.
Public TimeGet As Long is used to record the time spent in the input process Processing
Public TimePut As Long is used to record the time spent in processing the output process
Dim ColVal () As Byte 'is used to store the pixel value input from DIB
Dim ColOut () As Byte 'is used to store the pixel value output to DIB.
Dim InPutHei As Long is used to record the height of the input image
Dim InPutWid As Long is used to record the width of the input image
Dim bi24BitInfo As BitMapInfo 'defines BMP Information

We can see that I used two different dynamic arrays ColVal () and ColOut () in the input and output, which makes sense, because we do not just want to input and output images, processing pixels is also required. Including image scaling, color adjustment, sharpening, softening, and so on. Using two different arrays to store data separately is more conducive to program implementation.

Some impatient friends may have pasted the program into the project for trial use, but they will find that they cannot output images at all. This is because when the images you get with DIBGET are still in ColVal (), you need to put them in the ColOut () array to make the DIBPUT process take effect.

Here is a process for moving the entire array of data:

Public Sub CopyData (ByVal W As Long, ByVal H As Long)
Dim Length As Long
Dim I As Long
Dim L As Long
I = Bits \ 8
L = I-1
Length = (W + 1 &) * (H + 1 &) * I
ReDim ColOut (L, W, H)
CopyMemory ColOut (0, 0, 0), ColVal (0, 0, 0), Length
End sub

API definition:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)

In this case, we can try the following:

Adjust your monitor to 32-bit color.

Paste all the preceding API and variable definitions to a new module.

Create a new form and add two PICTURE controls: pictrue1 and picture2. One button is command1.

Load an image in pictrue1

Write the following code in command1:

Sub commandementclick ()
With picture1
. ScaleMode = 3
. BorderStyle = 0
DibGet. hdc, 0,. scalewidth,. scaleheight
End
CopyData InPutHei, InPutWid
Picture2.AutoRedraw = True
DibPut picture2.hdc
Picture2.refresh
End sub

Run it, press the button, and the image in pictreu1 is immediately displayed in picture2.

At this time, you may say that after such a long time, a picture will be posted? Can I use PaintPicture?

Yes. If you only need to post a picture, you don't have to worry about it. However, the image processing part we will talk about later will use the front-door pixel value. So this is just the beginning. What I really want to talk about is still in the future. Please stay tuned.

 

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.