函数参考


_WinAPI_SaveHBITMAPToFile

Saves a specified bitmap to the specified bitmap (.bmp) file.

#Include <WinAPIEx.au3>
_WinAPI_SaveHBITMAPToFile ( $sFile, $hBitmap [, $iXPelsPerMeter [, $iYPelsPerMeter]] )

参数

$sFile The name of the .bmp file in which to save the bitmap.
$hBitmap Handle to the bitmap to be save.
$iXPelsPerMeter [可选参数] The horizontal resolution, in pixels-per-meter.
$iYPelsPerMeter [可选参数] The vertical resolution, in pixels-per-meter.

返回值

成功: 返回 1.
失败: 返回 0 并设置 @error 标志为非 0 值.

注意/说明

This function saves the specified bitmap unchanged, except when the bitmap uses 32 bits-per-pixel color depth.
In this situation, if the bitmap contains an alpha channel, it will be saved as 32 bits-per-pixel image, and
as 24 bits-per-pixel image otherwise.

The horizontal and vertical resolutions pointed to by the $iXPelsPerMeter and $iYPelsPerMeter parameters has not
affects the image quality, but application can use this value to select a bitmap from a resource group that best
matches the characteristics of the current device. If the resolutions is not specified, the function uses the
current screen resolutions, usually 2834 pixels-per-meter which corresponds to 72 pixels-per-inch (dpi).

If the source bitmap is compressed, or use a color masks, the function fails.

相关

详情参考

None

示例/演示


#Include <APIConstants.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173

Global $hForm, $Pic, $hPic, $hDC, $hMemDC, $hMemSv, $hBitmap, $pBits, $hDev, $hObj, $Path
Global $aColorTable, $tColorTable

; Create array of colors of 256 entries required for 8 bits-per-pixel bitmap
Dim $aColorTable[256]
For $i = 0 To 255
    $aColorTable[$i] = _WinAPI_RGB(0, $i, 255 - $i)
Next

; Create color table from an array of colors
$tColorTable = _WinAPI_CreateDIBColorTable($aColorTable)

; Create 8 bits-per-pixel device-independent bitmap (DIB) and retrieve a pointer to the location of its bit values
$hBitmap = _WinAPI_CreateDIB(256, 256, 8, $tColorTable, 256)
$pBits = _WinAPI_GetExtended()

; Fill bitmap color indexes
For $i = 0 To 255
    _WinAPI_FillMemory($pBits + 256 * $i, 256, $i)
Next

; 创建 GUI
$hForm = GUICreate('MyGUI', 256, 256)
$Pic = GUICtrlCreatePic('', 0, 0, 256, 256)
$hPic = GUICtrlGetHandle($Pic)

; Create DDB from DIB to correct display in control
$hDC = _WinAPI_GetDC($hPic)
$hDev = _WinAPI_CreateCompatibleBitmap($hDC, 256, 256)
$hMemDC = _WinAPI_CreateCompatibleDC($hDC)
$hMemSv = _WinAPI_SelectObject($hMemDC, $hDev)
_WinAPI_DrawBitmap($hMemDC, 0, 0, $hBitmap)
_WinAPI_ReleaseDC($hPic, $hDC)
_WinAPI_SelectObject($hMemDC, $hMemSv)
_WinAPI_DeleteDC($hMemDC)

; Set bitmap to control
_SendMessage($hPic, $STM_SETIMAGE, 0, $hDev)
$hObj = _SendMessage($hPic, $STM_GETIMAGE)
If $hObj <> $hDev Then
    _WinAPI_DeleteObject($hDev)
EndIf

; Show GUI
GUISetState()

; Save 8 bits-per-pixel bitmap to .bmp file
$Path = FileSaveDialog('Save Image', @ScriptDir, 'Bitmap Image Files (*.bmp)', 2 + 16, @ScriptDir & '\MyImage.bmp', $hForm)
If $Path Then
    _WinAPI_SaveHBITMAPToFile($Path, $hBitmap, 2834, 2834)
EndIf

Do
Until GUIGetMsg() = -3