|
|
Title | Make an 8-bit device independent bitmap (DIB) from scratch |
Description | This example shows how to make an 8-bit device independent bitmap (DIB) from scratch in Visual Basic 6. It initializes the necessary structures and then calls the CreateDIBitmap API function. |
Keywords | DIB, CreateDIBitmap, bitmap, image |
Categories | Graphics, API |
|
|
Subroutine CreateDIB makes a new DIB. It initializes the BITMAPINFO_256 structure, gets the screen's DC, and calls CreateDIBitmap to make the DIB.
|
|
Dim Pixels() As Byte ' Pixel data.
Dim bm_info As BITMAPINFO_256 ' DIB bitmap info.
Dim hDIB As Long ' Bitmap handle.
Dim wid As Integer ' Size of the bitmap.
Dim hgt As Integer
' Create the DIB.
Private Sub CreateDIB()
Dim screen_hdc As Long
With bm_info.bmiHeader
.biSize = Len(bm_info.bmiHeader)
.biWidth = wid ' Width in pixels.
.biHeight = hgt ' Height in pixels.
.biPlanes = 1 ' 1 color plane.
.biBitCount = 8 ' 8 bits per pixel.
.biCompression = BI_RGB ' No compression.
.biSizeImage = 0 ' Unneeded with no
' compression.
.biXPelsPerMeter = 0 ' Unneeded.
.biYPelsPerMeter = 0 ' Unneeded.
.biClrUsed = 256 ' # colors in color table
' that are used by the image. 0 means all.
.biClrImportant = 256 ' # important colors. 0
' means all.
End With
' Get the screen's device context.
screen_hdc = GetDC(0)
' Create the DIB.
hDIB = CreateDIBitmap(screen_hdc, _
bm_info.bmiHeader, CBM_INIT, Pixels(0, 0), _
bm_info, DIB_RGB_COLORS)
End Sub
|
|
Subroutine SetColorTable initializes the DIB's color table to 256 shades of blue.
|
|
' Initialize 256 shades of blue.
Private Sub SetColorTable()
Dim i As Integer
For i = 0 To 255
bm_info.bmiColors(i).rgbRed = 0
bm_info.bmiColors(i).rgbGreen = 0
bm_info.bmiColors(i).rgbBlue = i
bm_info.bmiColors(i).rgbReserved = 0
Next i
End Sub
|
|
Subroutine SetPixels draws a picture on the DIB, covering it with shades of blue. For every 20th pixel vertically and horizontally, the code subtracts the color value from 255 to make part of a line.
|
|
' Create a drawing.
Private Sub SetPixels()
Dim X As Integer
Dim Y As Integer
wid = 100
hgt = 256
ReDim Pixels(0 To wid - 1, 0 To hgt - 1)
For Y = 0 To hgt - 1
For X = 0 To wid - 1
If Y Mod 20 = 19 Or X Mod 20 = 19 Then
Pixels(X, Y) = Y
Else
Pixels(X, Y) = 255 - Y
End If
Next X
Next Y
End Sub
|
|
Subroutine DrawDIB draws the DIB onto the program's form. It creates a compatible device context, uses SelectObject to copy the DIB into the device context, uses StretchBlt to copy the device context's picture onto the form, and calls DeleteDC to delete the device context.
|
|
' Draw the DIB onto the form.
Private Sub DrawDIB()
Dim compat_dc As Long
' Create a compatible device context.
compat_dc = CreateCompatibleDC(hdc)
' Select the DIB into the compatible DC.
SelectObject compat_dc, hDIB
' Copy the compatible DC's image onto the form.
StretchBlt Picture1.hdc, 0, 0, _
Picture1.ScaleWidth, Picture1.ScaleHeight, _
compat_dc, 0, 0, wid, hgt, _
SRCCOPY
' Destroy the compatible DC.
DeleteDC compat_dc
End Sub
|
|
|
|
|
|