Microsoft KB Archive/318188

= HOW TO: Save the Client Area of a Visual Basic Form to a DIB Section =

Article ID: 318188

Article Last Modified on 5/3/2006

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q318188



IN THIS TASK
SUMMARY
 * Save the Client Area to a DIB Section
 * Sample Code

REFERENCES



SUMMARY
This article demonstrates how to use the Graphics Device Interface (GDI) application programming interface (API) to save the client area of a Visual Basic form to a device-independent bitmap, known as a DIB section, that applications can write to directly. A DIB section, which you can create by calling the Windows API function CreateDIBSection, allows device-independent modification of the bits because you have access to a pointer to the location of the bitmap's bit values.

Save the Client Area to a DIB Section
To save the client area of a Visual Basic form to a DIB section, follow these steps:
 * 1) Use the GetDC GDI API call to create a device context for the Visual Basic form window's client area.
 * 2) Create a DIB section bitmap that is the same size as the Visual Basic form window's client area.
 * 3) Use the CreateCompatibleDC GDI API call to create a memory device context that is compatible with the Visual Basic form window's device context that you created in step 1.
 * 4) Use the SelectObject GDI API call to select the DIB section bitmap into the memory device context.
 * 5) Use a GDI API call, such as BitBlt or StretchBlt to transfer the image from the form's device context to the memory device context.

back to the top

Sample Code
To simplify the creation and use of DIB sections, a sample of code is available that contains most of the functionality in a dynamic link library (DLL). For additional information and to download this sample, click the article number below to view the article in the Microsoft Knowledge Base:

186221 SAMPLE: DibSectn.exe Uses DIBSections in Win32

The sample code that follows uses this DLL to demonstrate the steps that are listed earlier, and then calls DSStoreDIBSectionInBMPFile from the DLL to store the DIB section to a file. You can use other functions in the DLL to make changes to the DIB section that you create, or you can use GDI API calls. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source

Private Declare Function DSCreateDIBSection Lib &quot;dibsectn.dll&quot; ( _        ByVal dwX As Long, _         ByVal dwY As Long, _         ByVal wBits As Integer) _ As Long

Private Declare Function DSStoreDIBSectionInBMPFile Lib &quot;dibsectn.dll&quot; ( _        ByVal szFileName As String, _         ByVal hBitmap As Long) _ As Boolean Private Declare Function StretchBlt Lib &quot;gdi32&quot; (ByVal hdc As Long, ByVal x As Long, _        ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC _         As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, _         ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Private Declare Function GetDC Lib &quot;user32&quot; (ByVal hwnd As Long) As Long

Private Declare Function ReleaseDC Lib &quot;user32&quot; (ByVal hwnd As Long, _        ByVal hdc As Long) As Long

Private Declare Function CreateCompatibleDC Lib &quot;gdi32&quot; _ (ByVal hdc As Long) As Long Private Declare Function DeleteDC Lib &quot;gdi32&quot; _ (ByVal hdc As Long) As Long

Private Declare Function SelectObject Lib &quot;gdi32&quot; _ (ByVal hdc As Long, ByVal hObject As Long) As Long

Private Declare Function DeleteObject Lib &quot;gdi32&quot; _ (ByVal hObject As Long) As Long

Dim hdc As Long Dim DibSection As Long Dim hOldBitmap As Long Dim hdcMem As Long Dim Ret As Long hdc = GetDC(Form1.hwnd) hdcMem = CreateCompatibleDC(hdc)

' Call DSCreateDIBSection from the DLL. ' You can use the desired BPP in the last parameter(for example, 24) DibSection = DSCreateDIBSection(Form1.ScaleWidth, Form1.ScaleHeight, 24) hOldBitmap = SelectObject(hdcMem, DibSection)

Ret = StretchBlt(hdcMem, 0, 0, Form1.ScaleWidth, _              Form1.ScaleHeight, hdc, 0, 0, Form1.ScaleWidth, _               Form1.ScaleHeight, SRCCOPY) If Ret = 0 Then GoTo Error hOldBitmap = SelectObject(hdcMem, hOldBitmap)

' Call DSStoreDIBSectionInBMPFile from the DLL. ' Add error handling. Ret = DSStoreDIBSectionInBMPFile(&quot;c:\test.bmp&quot;, DibSection) If Ret = 0 Then GoTo Error Ret = ReleaseDC(Form1.hwnd, hdc) If Ret = 0 Then GoTo Error Ret = DeleteDC(hdcMem) If Ret = 0 Then GoTo Error Ret = DeleteObject(DibSection) If Ret = 0 Then GoTo Error Error: Debug.Print &quot;Error&quot;  ' Add error handling here. Success: Debug.Print &quot;Successful&quot; back to the top

