Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead and write an image's pixels using GetDIBits and SetDIBits
KeywordsGetDIBits, SetDIBits, pixels, RGB
CategoriesGraphics
 
Initialize a BITMAPINFO structure to describe the image. Redimension an array of Byte to hold pixel data. The array's entries will contain:

 pixels(1, X, Y)Blue component of pixel (X, Y)
 pixels(2, X, Y)Green component of pixel (X, Y)
 pixels(3, X, Y)Red component of pixel (X, Y)
 pixels(4, X, Y)Padding

The code then calls GetDIBits to fetch the data from a picture. It loops over all of the pixels, setting their blue color components to 255. This generally makes the image bluer.

It then loops over the pixels along a diagonal in the upper left quarter of the image. It sets the red, green, and blue components of those pixels to 0 to make them black. The result shows that the pixel indexing matches the normal PictureBox mapping where X increases from left to right and Y increases from top to bottom.

After it modifies the pixels, the code uses SetDIBits to copy the data into the output PictureBox. It finishes by setting picTo.Picture = picTo.Image to make the results visible.

 
Private Sub cmdGo_Click()
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim X As Integer
Dim Y As Integer

    ' Prepare the bitmap description.
    With bitmap_info.bmiHeader
        .biSize = 40
        .biWidth = picFrom.ScaleWidth
        ' Use negative height to scan top-down.
        .biHeight = -picFrom.ScaleHeight
        .biPlanes = 1
        .biBitCount = 32
        .biCompression = BI_RGB
        bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
            31) \ 32) * 4)
        pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
            * .biBitCount) + 7) \ 8)
        .biSizeImage = bytes_per_scanLine * Abs(.biHeight)
    End With

    ' Load the bitmap's data.
    ReDim pixels(1 To 4, 1 To picFrom.ScaleWidth, 1 To _
        picFrom.ScaleHeight)
    GetDIBits picFrom.hdc, picFrom.Image, _
        0, picFrom.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS

    ' Modify the pixels.
    For Y = 1 To picFrom.ScaleHeight
        For X = 1 To picFrom.ScaleWidth
            ' Set the blue component to 255.
            pixels(pixB, X, Y) = 255
        Next X
    Next Y

    For X = 1 To picFrom.ScaleHeight \ 2
        ' Make this pixel black.
        pixels(pixR, X, X) = 0
        pixels(pixG, X, X) = 0
        pixels(pixB, X, X) = 0
    Next X

    ' Display the result.
    SetDIBits picTo.hdc, picTo.Image, _
        0, picFrom.ScaleHeight, pixels(1, 1, 1), _
        bitmap_info, DIB_RGB_COLORS
    picTo.Picture = picTo.Image
End Sub
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated