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 it using a type structure
Keywordsmemory bitmap, bitmap, CreateCompatibleDC, CreateCompatibleBitmap, CreateFont, TextOut
CategoriesGraphics, API
 
The MakeMemoryBitmap function creates a device context, makes a bitmap, and uses SelectObject to attach the device context to the bitmap.
 
Private Type MemoryBitmap
    hDC As Long
    hBM As Long
    oldhDC As Long
    wid As Long
    hgt As Long
End Type

' Make a memory bitmap of the given size.
' Return the bitmap's DC.
Private Function MakeMemoryBitmap(ByVal wid As Long, ByVal _
    hgt As Long) As MemoryBitmap
Dim result As MemoryBitmap

    ' Create the device context.
    result.hDC = CreateCompatibleDC(0)

    ' Create the bitmap.
    result.hBM = CreateCompatibleBitmap(result.hDC, wid, _
        hgt)

    ' Make the device context use the bitmap.
    result.oldhDC = SelectObject(result.hDC, result.hBM)

    ' Return the MemoryBitmap structure.
    result.wid = wid
    result.hgt = hgt
    MakeMemoryBitmap = result
End Function
 
Subroutine DrawOnMemoryBitmap uses GDI functions to draw on the bitmap. This routine is application-specific.
 
' Draw on the memory bitmap.
Private Sub DrawOnMemoryBitmap(memory_bitmap As _
    MemoryBitmap)
Dim wid As Long
Dim hgt As Long

    wid = memory_bitmap.wid
    hgt = memory_bitmap.hgt

    ' Give the device context a white background.
    SelectObject memory_bitmap.hDC, _
        GetStockObject(WHITE_BRUSH)
    Rectangle memory_bitmap.hDC, 0, 0, wid, hgt
    SelectObject memory_bitmap.hDC, _
        GetStockObject(NULL_BRUSH)

    ' 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, wid, hgt
    MoveToEx memory_bitmap.hDC, 0, hgt, ByVal 0&
    LineTo memory_bitmap.hDC, wid, 0
End Sub
 
Subroutine DeleteMemoryBitmap deletes the bitmap and frees its resources.
 
' Delete the bitmap and free its resources.
Private Sub DeleteMemoryBitmap(memory_bitmap As _
    MemoryBitmap)
    SelectObject memory_bitmap.hDC, memory_bitmap.oldhDC
    DeleteObject memory_bitmap.hBM
    DeleteDC memory_bitmap.hDC
End Sub
 
The program's Form_Load event handler uses MakeMemoryBitmap to make a memory bitmap and DrawOnMemoryBitmap to draw on it. It copies the results into a PictureBox so you can see them and then uses DeleteMemoryBitmap to delete the bitmap.
 
Private Sub Form_Load()
Dim memory_bitmap As MemoryBitmap

    ' Create the memory bitmap.
    Picture1.ScaleMode = vbPixels
    memory_bitmap = MakeMemoryBitmap( _
        Picture1.ScaleWidth, _
        Picture1.ScaleHeight)

    ' Draw on the bitmap.
    DrawOnMemoryBitmap memory_bitmap

    ' Copy the device context into the PictureBox.
    Picture1.AutoRedraw = True
    BitBlt Picture1.hDC, 0, 0, _
        Picture1.ScaleWidth, _
        Picture1.ScaleHeight, _
        memory_bitmap.hDC, 0, 0, SRCCOPY
    Picture1.Picture = Picture1.Image

    ' Delete the memory bitmap.
    DeleteMemoryBitmap memory_bitmap
End Sub
 
Note that it would be relatively easy to convert the MemoryBitmap type into a class.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated