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
 
 
 
 
 
TitleCreate a bitmap in memory and draw on its individual pixels
Keywordsmemory bitmap, bitmap, CreateCompatibleDC, CreateCompatibleBitmap, pixels
CategoriesGraphics, API
 
This program uses GetDIBits to get the memory bitmap's pixel data. It modifies the data and saves it back into the bitmap using SetDIBits. The program finishes by drawing some lines on the bitmap.
 
' Draw on the memory bitmap.
Private Sub DrawOnMemoryBitmap(memory_bitmap As _
    MemoryBitmap)
Dim pixels() As Byte
Dim x As Integer
Dim Y As Integer
Dim R As Single
Dim dR As Single
Dim G As Single
Dim dG As Single
Dim i As Integer

    ' Load the bitmap's data into an array.
    ReDim pixels(1 To 4, _
        1 To memory_bitmap.wid, _
        1 To memory_bitmap.hgt)
    GetDIBits memory_bitmap.hdc, memory_bitmap.hBM, _
        0, memory_bitmap.hgt, pixels(1, 1, 1), _
        memory_bitmap.bitmap_info, DIB_RGB_COLORS

    ' Modify the pixels.
    R = 0
    dR = 255 / memory_bitmap.hgt
    dG = 255 / memory_bitmap.wid
    For Y = 1 To memory_bitmap.hgt
        G = 0
        For x = 1 To memory_bitmap.wid
            pixels(pixB, x, Y) = 255
            pixels(pixR, x, Y) = R
            pixels(pixG, x, Y) = G
            G = G + dG
        Next x
        R = R + dR
    Next Y

    ' Set some pixels halfway down the diagonal.
    For i = 1 To memory_bitmap.hgt \ 2 Step 10
        ' Make this block of pixels green.
        For x = 0 To 10
            For Y = 0 To 10
                pixels(pixR, i + x, i + Y) = 0
                pixels(pixG, i + x, i + Y) = 255
                pixels(pixB, i + x, i + Y) = 0
            Next Y
        Next x
    Next i

    ' Copy the results back into the memory bitmap.
    SetDIBits memory_bitmap.hdc, memory_bitmap.hBM, _
        0, memory_bitmap.hgt, pixels(1, 1, 1), _
        memory_bitmap.bitmap_info, DIB_RGB_COLORS

    ' Draw the on the device context.
    SelectObject memory_bitmap.hdc, _
        GetStockObject(BLACK_PEN)
    MoveToEx memory_bitmap.hdc, 0, 0, ByVal 0&
    LineTo memory_bitmap.hdc, memory_bitmap.wid, _
        memory_bitmap.hgt
    MoveToEx memory_bitmap.hdc, 0, memory_bitmap.hgt, ByVal _
        0&
    LineTo memory_bitmap.hdc, memory_bitmap.wid, 0
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated