#include <WinAPI.au3>
$sFile = @DesktopDir & '\Screen.bmp'
$X1 = 0
$X2 = 500
$Y1 = 0
$Y2 = 500
$hWnd = WinGetHandle("[CLASS:Progman]")
_ScreenCaptureToBMP($sFile, $X1, $X2, $Y1, $Y2, $hWnd)
ShellExecute($sFile)
Func _ScreenCaptureToBMP($sFile, $X1, $X2, $Y1, $Y2, $hWnd=0)
Local $hFile, $iMSize, $iSize, $iW, $iH
Local $stBuffer, $pBuffer, $iMod, $stMod_Buffer, $pMod_Buffer, $sData, $nBytes
Local $iOld_Opt = -1
Local $hKernel_Dll, $tWritten, $pWritten
$iW = $X2 - $X1 + 1
$iH = $Y2 - $Y1 + 1
#Region BMP Header part
$hFile = FileOpen($sFile, 16 + 2)
FileWrite($hFile, 'BM'); 2 bytes - 1-2
$iMSize = ($iW * 3 + Mod($iW, 4)) * $iH
$iSize = $iMSize + 54
FileWrite($hFile, Dec(Hex($iSize))); 4 bytes - 3-6
FileWrite($hFile, 0x0); 2x2 bytes - 7-10
FileWrite($hFile, 0x36); 4 bytes - 11-14
FileWrite($hFile, 40); 4 bytes - 15-18
FileWrite($hFile, Int($iW)); 4 bytes - 19-22
FileWrite($hFile, Int($iH)); 4 bytes - 23-26
FileWrite($hFile, Int(1572865)); 2x2 bytes; 2bytes - 1, 2 bytes-24; 27-30
FileWrite($hFile, Int(0)); 4 bytes - 31-34
FileWrite($hFile, Dec(Hex($iMSize))); 4 bytes - 35-38
FileWrite($hFile, 3780); 4 bytes - 39-42
FileWrite($hFile, 3780); 4 bytes - 43-46
FileWrite($hFile, Int(0)); #########; 4 bytes of zeros - 47-50
FileWrite($hFile, Int(0)); #########; 4 bytes of zeros - 50-54
FileClose($hFile)
#EndRegion BMP Header part
;
$hKernel_Dll = DllOpen("Kernel32.dll")
$stBuffer = DllStructCreate("byte[3]")
$pBuffer = DllStructGetPtr($stBuffer)
$tWritten = DllStructCreate("int Written")
$pWritten = DllStructGetPtr($tWritten)
$hFile = _WinAPI_CreateFile($sFile, 3)
_WinAPI_SetFilePointer($hFile, 0, 2)
$iMod = Mod($iW, 4)
If $iMod > 0 Then
$stMod_Buffer = DllStructCreate("byte[" & $iMod & "]")
DllStructSetData($stMod_Buffer, 1, Binary(0))
$pMod_Buffer = DllStructGetPtr($stMod_Buffer)
EndIf
If IsHWnd($hWnd) Then
$iOld_Opt = Opt("PixelCoordMode", 0)
EndIf
For $j = $Y2 To $Y1 Step -1
For $i = $X1 To $X2
$sData = Binary(PixelGetColor($i, $j, $hWnd))
DllStructSetData($stBuffer, 1, $sData)
_WinAPI_WriteFileEx($hFile, $pBuffer, 3, $nBytes, $tWritten, $pWritten, $hKernel_Dll)
Next
If $iMod > 0 Then
_WinAPI_WriteFileEx($hFile, $pMod_Buffer, $iMod, $nBytes, $tWritten, $pWritten, $hKernel_Dll)
EndIf
Next
DllClose($hKernel_Dll)
_WinAPI_CloseHandle($hFile)
If $iOld_Opt <> -1 Then Opt("PixelCoordMode", $iOld_Opt)
EndFunc
Func _WinAPI_WriteFileEx($hFile, $pBuffer, $iToWrite, ByRef $iWritten, $tWritten = 0, $pWritten = 0, $vKernel_Dll = 'Kernel32.dll')
Local $aResult = DllCall($vKernel_Dll, "int", "WriteFile", "hwnd", $hFile, "ptr", $pBuffer, _
"uint", $iToWrite, "ptr", $pWritten, "ptr", 0)
$iWritten = DllStructGetData($tWritten, "Written")
Return $aResult[0] <> 0
EndFunc