計(jì)算機(jī)二級(jí)VisualBasic輔導(dǎo):VB圖像處理之像素的獲取和輸出

字號(hào):

要處理一個(gè)圖像,首先要獲得該圖像的像素值,而VB本身提供的PICTURE控件雖然可以打開很多類型的圖片,但是它提供的那個(gè)POINT方法讀取像素實(shí)在是太慢。而使用GetPixel這個(gè)API的速度也快不到哪里去,因?yàn)镻IONT方法本身就是對(duì)于GetPixel的一個(gè)包裝。
    在VB中要快速獲取一幅在PICTURE中打開的圖像比較快速的方法是使用DIB方法,當(dāng)然還有DDB方法,不過使用DDB方法還需要考慮不同顏色深度的圖像的分別處理,在程序的實(shí)現(xiàn)上要相對(duì)復(fù)雜,而使用DIB方法則不必,并且在處理速度上比DDB方法也慢的有限。
    過程一:獲得一個(gè)在PICTURE控件中打開的圖像的所有像素。
    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 With
    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 "錯(cuò)誤號(hào):" & Err.Number & ":" & Err.Description
    End Sub