# Function Reference

# _WinAPI_CompressBitmapBits

Creates a compressed data block from the specified bitmap

#include <WinAPIGdi.au3>
_WinAPI_CompressBitmapBits ( $hBitmap, ByRef $pBuffer [, $iCompression = $COMPRESSION_BITMAP_PNG [, $iQuality = 100]] )

## Parameters

$hBitmap | A handle to the bitmap to be compressed.
$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 | [optional] The compression method. This parameter can be one of the following values. $COMPRESSION_BITMAP_PNG (Default) $COMPRESSION_BITMAP_JPEG
$iQuality | [optional] The quality of JPEG image, in percent. This value is ignored for non JPEG compression. Default is 100.

## Return Value

Success: | The number of bytes copied to the buffer.
Failure: | 0 and sets the @error flag to non-zero.

## Remarks

In fact, the data that returns this function represent .jpeg or .png image file in binary form. You can use the _WinAPI_WriteFile() function to create the appropriate image file directly from this data.
The returned data always represent a 24-bit color depth .jpeg image, or 32-bit color depth (with or without alpha chanel) .png image independent on the color depth of the source bitmap.
When you no longer need the buffer allocated by this function, you must call the _WinAPI_FreeMemory() function (do not use any other memory routines) to release occupied memory.
This function internally uses the GDI+ DLL library.

## Related

_WinAPI_FreeMemory, _WinAPI_WriteFile

## Example

    #include "Extras\HelpFileInternals.au3"

    #include <MsgBoxConstants.au3>
    #include <WinAPIFiles.au3>
    #include <WinAPIGdi.au3>
    #include <WinAPIHObj.au3>
    #include <WinAPIMem.au3>
    #include <WinAPIRes.au3>

    ; Load image
    Local $sBmp = _Extras_PathFull('AutoIt.bmp')
    Local $hSource = _WinAPI_LoadImage(0, $sBmp, $IMAGE_BITMAP, 0, 0, BitOR($LR_LOADFROMFILE, $LR_CREATEDIBSECTION))

    ; Resize bitmap to 256x256 pixels
    Local $hBitmap = _WinAPI_AdjustBitmap($hSource, 256, 256, $HALFTONE)

    ; Create compressed PNG data
    Local $pData = 0
    Local $iLength = _WinAPI_CompressBitmapBits($hBitmap, $pData, 2) ;$COMPRESSION_BITMAP_PNG)

    ; Create .ico file
    If Not @error Then
        Local $tICO = DllStructCreate('align 1;ushort Reserved;ushort Type;ushort Count;byte Header[20]')
        Local $tHDR = DllStructCreate('byte Width;byte Height;byte ColorCount;byte Reserved;ushort Planes;ushort BitCount;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, 'ColorCount', 0)
        DllStructSetData($tHDR, 'Reserved', 0)
        DllStructSetData($tHDR, 'Planes', 1)
        DllStructSetData($tHDR, 'BitCount', 32)
        DllStructSetData($tHDR, 'Size', $iLength)
        DllStructSetData($tHDR, 'Offset', DllStructGetSize($tICO))
        Local $hFile = _WinAPI_CreateFile(@TempDir & '\MyIcon.ico', 1, 4)
        Local $iBytes
        _WinAPI_WriteFile($hFile, $tICO, DllStructGetSize($tICO), $iBytes)
        _WinAPI_WriteFile($hFile, $pData, $iLength, $iBytes)
        _WinAPI_CloseHandle($hFile)
        ShellExecute("MsPaint", '"' & @TempDir & '\MyIcon.ico' & '"')
    Else
        MsgBox($MB_SYSTEMMODAL, "Error", "_WinAPI_CompressBitmapBits() failed (@error = " & @error & ")")
    EndIf

    ; Delete unnecessary bitmaps
    _WinAPI_DeleteObject($hSource)
    _WinAPI_DeleteObject($hBitmap)

    ; Free memory
    _WinAPI_FreeMemory($pData)