你要这样来验証呀,看来用GDI+更明显点
#include <ButtonConstants.au3>
#include <Memory.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
Main()
Func Main()
Local $HBITMAP, $HBITMAPbak, $bImage, $hGUI, $iButton, $hButton, $iPic, $hHBITMAP, $msg, $JPEG_Quality = 100
_GDIPlus_Startup()
$HBITMAP = _ScreenCapture_Capture("", 0, 0, 100, 100)
$HBITMAPbak = $HBITMAP
$HBITMAP = _GDIPlus_BitmapCreateFromHBITMAP($HBITMAP)
$bImage = HBitmap2Binary($HBITMAP, $JPEG_Quality)
_ScreenCapture_SetJPGQuality($JPEG_Quality)
_ScreenCapture_SaveImage("f:\a.jpg", $HBITMAPbak)
$hGUI = GUICreate("Test", 300, 300)
$iButton = GUICtrlCreateButton("", 0, 0, 100, 100, $BS_BITMAP)
$iPic = GUICtrlCreatePic("f:\a.jpg", 110, 0, 100, 100)
$hButton = GUICtrlGetHandle($iButton)
$hHBITMAP = Load_BMP_From_Mem($bImage, True)
_SendMessage($hButton, $BM_SETIMAGE, 0, $hHBITMAP)
_GDIPlus_Shutdown()
GUISetState(@SW_SHOW, $hGUI)
While True
$msg = GUIGetMsg()
Switch $msg
Case -3, $iButton
ExitLoop
EndSwitch
WEnd
_WinAPI_DeleteObject($hHBITMAP)
GUIDelete($hGUI)
EndFunc
Func Load_BMP_From_Mem($mem_image, $hHBITMAP = False)
If Not IsBinary($mem_image) Then Return SetError(1, 0, 0)
Local $declared = True
If Not $ghGDIPDll Then
_GDIPlus_Startup()
$declared = False
EndIf
Local Const $memBitmap = Binary($mem_image)
Local Const $len = BinaryLen($memBitmap)
Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE)
Local Const $pData = _MemGlobalLock($hData)
Local $tMem = DllStructCreate("byte[" & $len & "]", $pData)
DllStructSetData($tMem, 1, $memBitmap)
_MemGlobalUnlock($hData)
Local $hStream = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0)
$hStream = $hStream[3]
Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0)
$hBitmap = $hBitmap[2]
Local Const $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
"dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT))
$tMem = 0
If $hHBITMAP Then
Local Const $hHBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
_GDIPlus_BitmapDispose($hBitmap)
If Not $declared Then _GDIPlus_Shutdown()
Return $hHBmp
EndIf
If Not $declared Then _GDIPlus_Shutdown()
Return $hBitmap
EndFunc
Func HBitmap2Binary($hBitmap, $JPEG_Quality = 100)
Local $aStream, $Stream, $JpgGUID, $tagGUID, $Ptr, $tParams, $pParams, $tData, $pData, $aMemory, $Memory, $MemSize, $MemPtr, $dStruct, $bString, $tStruct, $aCall
Local $declared = True
If Not $ghGDIPDll Then
_GDIPlus_Startup()
$declared = False
EndIf
$aStream = DllCall("ole32.dll", "uint", "CreateStreamOnHGlobal", "ptr", 0, "bool", 1, "ptr*", 0)
$Stream = $aStream[3]
$JpgGUID = _GDIPlus_EncodersGetCLSID("JPG")
$tagGUID = _WinAPI_GUIDFromString($JpgGUID)
$Ptr = DllStructGetPtr($tagGUID)
$tParams = _GDIPlus_ParamInit(1)
$tData = DllStructCreate("int Quality")
DllStructSetData($tData, "Quality", $JPEG_Quality)
$pData = DllStructGetPtr($tData)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
$pParams = DllStructGetPtr($tParams)
DllCall($ghGDIPDll, "uint", "GdipSaveImageToStream", "ptr", $hBitmap, "ptr", $Stream, "ptr", $Ptr, "ptr", $pParams)
$tData = 0
$tParams = 0
$aMemory = DllCall("ole32.dll", "uint", "GetHGlobalFromStream", "ptr", $Stream, "ptr*", 0)
$Memory = $aMemory[2]
$MemSize = _MemGlobalSize($Memory)
$MemPtr = _MemGlobalLock($Memory)
$dStruct = DllStructCreate("byte[" & $MemSize & "]", $MemPtr)
$bString = DllStructGetData($dStruct, 1)
$tStruct = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
$aCall = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $Stream, "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tStruct))
_MemGlobalFree($Memory)
If Not $declared Then _GDIPlus_Shutdown()
Return $bString
EndFunc
|