Function Reference


_GDIPlus_BitmapLockBits

Locks a portion of a bitmap for reading or writing

#include <GDIPlus.au3>
_GDIPlus_BitmapLockBits ( $hBitmap, $iLeft, $iTop, $iWidth, $iHeight [, $iFlags = $GDIP_ILMREAD [, $iFormat = $GDIP_PXF32RGB]] )

Parameters

$hBitmap Handle to a bitmap object
$iLeft X coordinate of the upper-left corner of the rectangle to lock
$iTop Y coordinate of the upper-left corner of the rectangle to lock
$iWidth The width of the rectangle to lock
$iHeight The height of the rectangle to lock
$iFlags [optional] Set of flags that specify whether the locked portion of the bitmap is available for reading or for writing and whether the caller has already allocated a buffer. Can be a combination of the following:
    $GDIP_ILMREAD - A portion of the image is locked for reading
    $GDIP_ILMWRITE - A portion of the image is locked for writing
    $GDIP_ILMUSERINPUTBUF - The buffer is allocated by the user
$iFormat [optional] Specifies the format of the pixel data in the temporary buffer. Can be one of the following:
    $GDIP_PXF01INDEXED - 1 bpp, indexed
    $GDIP_PXF04INDEXED - 4 bpp, indexed
    $GDIP_PXF08INDEXED - 8 bpp, indexed
    $GDIP_PXF16GRAYSCALE - 16 bpp, grayscale
    $GDIP_PXF16RGB555 - 16 bpp; 5 bits for each RGB
    $GDIP_PXF16RGB565 - 16 bpp; 5 bits red, 6 bits green, and 5 bits blue
    $GDIP_PXF16ARGB1555 - 16 bpp; 1 bit for alpha and 5 bits for each RGB component
    $GDIP_PXF24RGB - 24 bpp; 8 bits for each RGB
    $GDIP_PXF32RGB - 32 bpp; 8 bits for each RGB. No alpha.
    $GDIP_PXF32ARGB - 32 bpp; 8 bits for each RGB and alpha
    $GDIP_PXF32PARGB - 32 bpp; 8 bits for each RGB and alpha, pre-multiplied

Return Value

Success: a $tagGDIPBITMAPDATA structure.
Failure: sets the @error flag to non-zero.

Remarks

When you are done with the locked portion, call _GDIPlus_BitmapUnlockBits() to release the locked region.

Related

_GDIPlus_ImageGetPixelFormat, _WinAPI_DeleteObject

See Also

Search GdipBitmapLockBits in MSDN Library.

Example

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
        ; X64 running support
        Local $sWow64 = ""
        If @AutoItX64 Then $sWow64 = "\Wow6432Node"

        ;get AutoIt install dir
        Local $sRegPath = "HKLM\SOFTWARE" & $sWow64 & "\AutoIt v3\AutoIt"

        Local $sFile = RegRead($sRegPath, "InstallDir") & "\Examples\GUI\logo4.gif"
        If Not FileExists($sFile) Then
                MsgBox(($MB_SYSTEMMODAL + $MB_ICONHAND), "", $sFile & " not found!", 30)
                Return False
        EndIf

        _GDIPlus_Startup()
        Local $hImage = _GDIPlus_ImageLoadFromFile($sFile) ;create an image object based on a file
        If @error Then
                _GDIPlus_Shutdown()
                MsgBox(($MB_SYSTEMMODAL + $MB_ICONHAND), "", "An error has occured - unable to load image!", 30)
                Return False
        EndIf

        Local $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage) ;get width and height of the image
        Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
        Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
        _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iW, $iH)

        Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32ARGB) ;locks a portion of a bitmap for reading and writing. More infor at http://msdn.microsoft.com/en-us/library/windows/desktop/ms536298(v=vs.85).aspx
        Local $iScan0 = DllStructGetData($tBitmapData, "Scan0") ;get scan0 (pixel data) from locked bitmap
        Local $iSearchPixel = Int(0xFF000080), $iReplaceColor = 0x00000000 ;color format 0xAARRGGBB
        Local $tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)
        Local $iPixel, $iRowOffset

        For $iY = 0 To $iH - 1
                $iRowOffset = $iY * $iW + 1
                For $iX = 0 To $iW - 1 ;get each pixel in each line and row
                        $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX) ;get pixel color
                        If $iPixel = $iSearchPixel Then DllStructSetData($tPixel, 1, $iReplaceColor, $iRowOffset + $iX) ;compare and replace pixel color (blue with transparent color)
                Next
        Next
        _GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData) ;unlocks a portion of a bitmap that was locked by _GDIPlus_BitmapLockBits

        ;to save manipulated image just use _GDIPlus_ImageSaveToFile()

        ShellExecute($sFile) ;display original image just to compare with manipulated one

        ;display manipulated image
        Local $hGUI = GUICreate("_GDIPlus_BitmapLockBits Demo", $iW, $iH)
        GUISetState(@SW_SHOW)

        Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a Graphics object from a window handle
        _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH) ;copy manipulated image to graphics handle

        Do
        Until GUIGetMsg() = $GUI_EVENT_CLOSE

        ;cleanup resources
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_GraphicsDispose($hContext)
        _GDIPlus_BitmapDispose($hBitmap)
        _GDIPlus_GraphicsDispose($hGraphic)
        _GDIPlus_Shutdown()
        GUIDelete($hGUI)
EndFunc   ;==>Example