从指定位图中创建压缩数据块
#Include <WinAPIEx.au3>
_WinAPI_CompressBitmapBits ( $hBitmap, ByRef $pBuffer [, $iCompression [, $iQuality]] )
$hBitmap | 要被压缩的位图的句柄 |
$pBuffer | A pointer to a memory block (buffer) that receives the compressed data. Optionaly, you can set this parameter to 0 before function call, then the function will allocate the required memory block itself. Otherwise, it must be a valid memory pointer returned by the _WinAPI_CreateBuffer() function, or by previously calling this function. |
$iCompression | [可选参数] 压缩方法. 可为下列值之一: $COMPRESSION_BITMAP_PNG $COMPRESSION_BITMAP_JPEG |
$iQuality | [可选参数] JPEG图像的百分比质量. 无压缩时忽略. |
成功: | 保存在$tBuffer中的压缩数据的字节长度. |
失败: | 返回 0并设置@error非0 |
#Include <APIConstants.au3>
#Include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global $tICO, $tHDR, $hFile, $hBitmap, $hSource, $pData = 0, $Bytes, $Length
; Load image
$hSource = _WinAPI_LoadImage(0, @ScriptDir & '\Extras\AutoIt.bmp', $IMAGE_BITMAP, 0, 0, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))
; Resize bitmap to 256x256 pixels
$hBitmap = _WinAPI_AdjustBitmap($hSource, 256, 256, $HALFTONE)
; Create compressed PNG data
$Length = _WinAPI_CompressBitmapBits($hBitmap, $pData)
; Create .ico file
If Not @error Then
$tICO = DllStructCreate('align 1;ushort Reserved;ushort Type;ushort Count;byte Header[20]')
$tHDR = DllStructCreate('byte Width;byte Height;byte Colors;byte Reserved;word Planes;word BPP;long Size;long Offset', DllStructGetPtr($tICO, 'Header'))
DllStructSetData($tICO, 'Reserved', 0)
DllStructSetData($tICO, 'Type', 1)
DllStructSetData($tICO, 'Count', 1)
DllStructSetData($tHDR, 'Width', 0)
DllStructSetData($tHDR, 'Height', 0)
DllStructSetData($tHDR, 'Colors', 0)
DllStructSetData($tHDR, 'Reserved', 0)
DllStructSetData($tHDR, 'Planes', 1)
DllStructSetData($tHDR, 'BPP', 32)
DllStructSetData($tHDR, 'Size', $Length)
DllStructSetData($tHDR, 'Offset', DllStructGetSize($tICO))
$hFile = _WinAPI_CreateFile(@ScriptDir & '\MyIcon.ico', 1, 4)
_WinAPI_WriteFile($hFile, DllStructGetPtr($tICO), DllStructGetSize($tICO), $Bytes)
_WinAPI_WriteFile($hFile, $pData, $Length, $Bytes)
_WinAPI_CloseHandle($hFile)
EndIf
; Delete unnecessary bitmaps
_WinAPI_DeleteObject($hSource)
_WinAPI_DeleteObject($hBitmap)
; Free memory
_WinAPI_FreeMemory($pData)