Follow the steps:
- retrieve width, height, bitperpixel values
- create and populate BITMAPINFO structure
- allocate memory for an array of bits
- call GetDIBits API function
There is a technique of calling the GetDIBits twice, which eliminates steps 1 and 2.
- create and populate BITMAPFILEHEADER structure
- create empty file and start appending to it
........- BITMAPFILEHEADER data
........- BITMAPINFO data and possibly COLORREF table
........- array of bits
- close the file
- release memory allocated for the array of bits
- voila!
When I was trying to solve this problem, I just picked commented C code from Windows 2000 Programming manual and followed the steps. Read and understand bitmap file structure first of all. This information is widely available on the Internet.
* * *
Though today my choice would be not GDI but rather Metafile API functions and of course GDI+. For FoxPro developers GDI+ is available through flat API calls. Very often these calls hide a lot of complexity under the hood. For example:
DECLARE INTEGER GdipSaveImageToFile IN gdiplus;
INTEGER img, STRING filename,;
STRING clsidEncoder, INTEGER encoderParams
This function exposes very simple interface:
img -- handle to GDI+ image object that can be easily created from a disk file via GdipLoadImageFromFile call
filename -- target file name, supports BMP, PNG, GIF, JPG, TIF, EMF formats
clsidEncoder -- GUID for selected image encoder, fixed value for each graphics format, 16 bytes
encoderParams -- set to 0 for simplicity
* * *
Some code samples from my web site (members area):
GDI+: custom GDI+ classhttp://www.news2news.com/vfp/?example=450Storing screen shot of a form to a bitmap filehttp://www.news2news.com/vfp/?example=187Storing screen shot of a form to enhanced metafile (*.emf)http://www.news2news.com/vfp/?example=402