About Monkey 2 › Forums › Monkey 2 Code Library › Capture screen to pixmap (Windows)
This topic contains 0 replies, has 1 voice, and was last updated by 
 Hezkore
 1 year, 10 months ago.
		Viewing 1 post (of 1 total)
	
	- 
		AuthorPosts
 - 
		
			
				
June 17, 2017 at 2:00 pm #8797
This was originally written by Mark.
It lets you capture the entire screen to a pixmap.This example saves the pixmap to your desktop:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109#Import "<std>"Using std..#Import "<windows.h>"#Import "<libuser32.a>"ExternAlias DWORD:UIntAlias HGDIOBJ:Void PtrStruct HDC__EndAlias HDC:HDC__ PtrStruct HWND__EndAlias HWND:HWND__ PtrStruct HBITMAP__EndAlias HBITMAP:HBITMAP__ PtrStruct BITMAPINFOHEADERField biSize:DWORDField biWidth:IntField biHeight:IntField biPlanes:ShortField biBitCount:ShortField biCompression:DWORDEndStruct BITMAPINFOField bmiHeader:BITMAPINFOHEADEREndConst SM_CYVIRTUALSCREEN:IntConst SM_CXVIRTUALSCREEN:IntConst SRCCOPY:DWORDConst NOTSRCCOPY:DWORDConst CAPTUREBLT:DWORDConst DIB_PAL_COLORS:UIntConst DIB_RGB_COLORS:UIntConst BI_RGB:DWORDFunction GetDesktopWindow:HWND()Function GetDC:HDC(hwnd:HWND)Function CreateCompatibleDC:HDC(hdc:HDC)Function GetSystemMetrics:int(index:Int)Function CreateCompatibleBitmap:HBITMAP(hdc:HDC,width:Int,height:Int)Function SelectObject:HGDIOBJ(hdc:HDC,hgdiobj:HGDIOBJ)Function DeleteObject:Int( hgdiobj:HGDIOBJ )Function BitBlt:Int(hdcDest:HDC,nXDest:Int,nYDest:Int,nWidth:Int,nHeight:Int,hdcSrc:HDC,nXSrc:Int,nYSrc:Int,dwRop:DWORD)Function ReleaseDC:Int(hwnd:HWND,hdc:HDC)Function DeleteDC:Int(hdc:HDC)Function GetDIBits:Int( hdc:HDC,hbmp:HBITMAP,firstLine:UInt,numlines:UInt,bitmapData:Void Ptr,bitmapInfo:BITMAPINFO Ptr,usage:UInt )PublicFunction CaptureWindow:Pixmap()Local hwnd:=GetDesktopWindow()Local w:=GetSystemMetrics( SM_CXVIRTUALSCREEN )Local h:=GetSystemMetrics( SM_CYVIRTUALSCREEN )Local hwnddc:=GetDC( hwnd )Local hcapturedc:=CreateCompatibleDC( hwnddc )Local hcapturebmp:=CreateCompatibleBitmap( hwnddc,w,h )SelectObject( hcapturedc,hcapturebmp )BitBlt( hcapturedc,0,0,w,h,hwnddc,0,0,SRCCOPY|CAPTUREBLT )Local bmpInfo:BITMAPINFOlibc.memset( Varptr bmpInfo,0,libc.sizeof( bmpInfo ) )bmpInfo.bmiHeader.biSize = libc.sizeof( bmpInfo.bmiHeader )bmpInfo.bmiHeader.biWidth = wbmpInfo.bmiHeader.biHeight = -hbmpInfo.bmiHeader.biPlanes = 1bmpInfo.bmiHeader.biBitCount = 24bmpInfo.bmiHeader.biCompression = BI_RGBLocal pixmap:=New Pixmap( w,h,PixelFormat.RGB8 )GetDIBits( hcapturedc,hcapturebmp,0,h,pixmap.Data,Varptr bmpInfo,DIB_RGB_COLORS )For Local y:=0 Until hLocal p:=pixmap.PixelPtr( 0,y )For Local x:=0 Until wLocal t:=p[0]p[0]=p[2]p[2]=tp+=3NextNextDeleteDC( hcapturedc )DeleteObject( hcapturebmp )Return pixmapEndFunction Main()Local pm:=CaptureWindow()Print "Captured! w="+pm.Width+", h="+pm.Heightpm.Save( DesktopDir()+"test.png" )End - 
		AuthorPosts
 
		Viewing 1 post (of 1 total)
	
	You must be logged in to reply to this topic.