WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Save Screen to a BMP File

 Keywords: portion screen file bmp  
You can use this function to save a screen portion to a bmp file.

It's interesting because I think it could be used to make bitmaps using the gdi functions, maybe to display simple graphics reports in a dialog. or even to stamp text in an existing bitmap and save it.

;--------------------------------------------------------------------;
;SaveScreenToBMP : Saves a screen portion to a 24 bit uncompressed   ;
;                  bitmap file.                                      ;
;--------------------------------------------------------------------;
;x      : x screen coordinate                                        ;
;y      : y screen coordinate                                        ;
;bw     : width of screen portion                                    ;
;bh     : height of screen portion                                   ;
;bitmap : bitmap file to create                                      ;
;--------------------------------------------------------------------;
;Guido 04/03                                                         ;
;Ref: http://users.ece.gatech.edu/~slabaugh/personal/c/bmpwrite.html ;
;     http://www.jeffheaton.com/source/sbitmap.c                     ;
;--------------------------------------------------------------------;
#definefunction SaveScreenToBMP(x,y,bw,bh,bitmap)
gdi32=dllload(strcat(dirwindows(1),"gdi32.dll"))
user32=dllload(strcat(dirwindows(1),"user32.dll"))

SRCCOPY=13369376
DIB_RGB_COLORS=0

;structure sizes
szBITMAPINFOHEADER=40
szBITMAPFILEHEADER=14

;desktop DC
hwscreen=dllcall(user32,long:"GetDesktopWindow")
hdc=dllcall(user32,long:"GetDC",long:hwscreen)

hmdc=dllcall(gdi32,long:"CreateCompatibleDC",long:hdc)

;24 bit bitmap
hbm=dllcall(gdi32,long:"CreateBitmap",long:bw,long:bh,long:1,long:24,lpnull)

dllcall(gdi32,long:"SelectObject",long:hmdc,long:hbm)

;copy screen portion to bitmap
dllcall(gdi32,long:"BitBlt",long:hmdc,long:0,long:0,long:bw,long:bh,long:hdc,long:x,long:y,long:SRCCOPY)

;write bitmap
szinfo=szBITMAPINFOHEADER
info=binaryalloc(szinfo) ;BITMAPINFO 
binarypoke4(info,0,szBITMAPINFOHEADER) ;biSize

;fill BITMAPINFO 
dllcall(gdi32,long:"GetDIBits",long:hdc,long:hbm,long:0,long:0,lpnull,lpbinary:info,long:DIB_RGB_COLORS)
binaryeodset(info,szinfo)

biSizeImage=binarypeek4(info,20)
bits=binaryalloc(biSizeImage)

;get bitmap bits
dllcall(gdi32,long:"GetDIBits",long:hdc,long:hbm,long:0,long:bh,lpbinary:bits,lpbinary:info,long:DIB_RGB_COLORS)
binaryeodset(bits,biSizeImage)

;header
finfo=binaryalloc(szBITMAPFILEHEADER)
binarypokestr(finfo,0,"BM") ;bfType
binarypoke4(finfo,10,szBITMAPFILEHEADER+szBITMAPINFOHEADER) ;bfOffBits
;bfSize=0
;bfReserved1=0
;bfReserved2=0

;write file
BinaryWriteEx(finfo,0,bitmap,0,szBITMAPFILEHEADER)
BinaryWriteEx(info,0,bitmap,szBITMAPFILEHEADER,szBITMAPINFOHEADER)
BinaryWriteEx(bits,0,bitmap,szBITMAPFILEHEADER+szBITMAPINFOHEADER,biSizeImage)

;free
binaryfree(finfo)
binaryfree(info)
binaryfree(bits)

dllcall(user32,long:"ReleaseDC",long:hwscreen,long:hdc)
dllcall(gdi32,long:"DeleteObject",long:hbm)

dllfree(gdi32)
dllfree(user32)
#endfunction

Article ID:   W15998
File Created: 2004:03:30:15:42:08
Last Updated: 2004:03:30:15:42:08