# Function Reference

# _GDIPlus_GraphicsDrawImageRect

Draws an image at a specified location

#include <GDIPlus.au3>
_GDIPlus_GraphicsDrawImageRect ( $hGraphics, $hImage, $nX, $nY, $nW, $nH )

## Parameters

$hGraphics | Handle to a Graphics object
$hImage | Handle to an Image object
$nX | The X coordinate of the upper left corner of the rendered image
$nY | The Y coordinate of the upper left corner of the rendered image
$nW | Specifies the width of the destination rectangle at which to draw the image
$nH | Specifies the height of the destination rectangle at which to draw the image

## Return Value

Success: | True.
Failure: | False and sets the @error flag to non-zero, @extended may contain GPSTATUS error code ($GDIP_ERR* see GPIPlusConstants.au3).

## See Also

Search GdipDrawImageRect in MSDN Library.

## Example

### Example 1

    #include <GDIPlus.au3>
    #include <ScreenCapture.au3>
    #include <WinAPIHObj.au3>

    Example()

    Func Example()
        Local $hBitmap1, $hBitmap2, $hImage1, $hImage2, $hGraphic

        ; Initialize GDI+ library
        _GDIPlus_Startup()

        ; Capture full screen
        $hBitmap1 = _ScreenCapture_Capture("")
        $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap1)

        ; Capture screen region
        $hBitmap2 = _ScreenCapture_Capture("", 0, 0, 400, 300)
        $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)

        ; Draw one image in another
        $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
        _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 100, 100, 400, 300)

        ; Draw a frame around the inserted image
        _GDIPlus_GraphicsDrawRect($hGraphic, 100, 100, 400, 300)

        ; Save resultant image
        _GDIPlus_ImageSaveToFile($hImage1, @MyDocumentsDir & "\GDIPlus_Image.jpg")

        ; Clean up resources
        _GDIPlus_ImageDispose($hImage1)
        _GDIPlus_ImageDispose($hImage2)
        _WinAPI_DeleteObject($hBitmap1)
        _WinAPI_DeleteObject($hBitmap2)

        ; Shut down GDI+ library
        _GDIPlus_Shutdown()

        ShellExecute(@MyDocumentsDir & "\GDIPlus_Image.jpg")
    EndFunc   ;==>Example

### Example 2

    #include <GDIPlus.au3>
    #include <GUIConstantsEx.au3>
    #include <ScreenCapture.au3>
    #include <WinAPIHObj.au3>

    Global $g_hGUI, $g_hGfxCtxt, $g_hBitmap, $g_hBMP, $g_hGraphics

    Example()

    Func Example()
        AutoItSetOption("GUIOnEventMode", 1)

        _GDIPlus_Startup() ;initialize GDI+
        Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBGColor format RRGGBB

        $g_hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
        GUISetBkColor($iBgColor, $g_hGUI) ;set GUI background color
        GUISetState(@SW_SHOW)

        ;create buffered graphics frame set for smoother gfx object movements
        $g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($g_hGUI) ;create a graphics object from a window handle
        $g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $g_hGraphics)
        $g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap)
        Local Const $iW = 300, $iH = 300
        Local $hHBmp = _ScreenCapture_Capture("", 0, @DesktopHeight - $iH, $iW, @DesktopHeight) ;create a GDI bitmap by capturing an area on desktop
        $g_hBMP = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
        _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
        Local $iVectorX = Random(1.5, 2.5), $iVectorY = Random(1.5, 2.5) ;define x and y vector
        Local $iX = 0.0, $iY = 0.0 ;define start coordinate

        GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

        Do
            _GDIPlus_GraphicsClear($g_hGfxCtxt, 0xFF000000 + $iBgColor) ;clear bitmap for repaint
            _GDIPlus_GraphicsDrawImageRect($g_hGfxCtxt, $g_hBMP, $iX, $iY, $iW, $iH) ;draw bitmap to backbuffer
            _GDIPlus_GraphicsDrawImageRect($g_hGraphics, $g_hBitmap, 0, 0, $iWidth, $iHeight) ;copy drawn bitmap to graphics handle (GUI)
            $iX += $iVectorX ;add x vector to current x position
            $iY += $iVectorY ;add y vector to current y position
            If $iX < 0 Or $iX > ($iWidth - $iW) Then $iVectorX *= -1 ;when x border is reached reverse x vector
            If $iY < 0 Or $iY > ($iHeight - $iH) Then $iVectorY *= -1 ;when y border is reached reverse y vector
        Until Not Sleep(20) ;sleep 20 ms to avoid high cpu usage
    EndFunc   ;==>Example

    Func _Exit()
        ;cleanup GDI+ resources
        _GDIPlus_GraphicsDispose($g_hGfxCtxt)
        _GDIPlus_GraphicsDispose($g_hGraphics)
        _GDIPlus_BitmapDispose($g_hBitmap)
        _GDIPlus_BitmapDispose($g_hBMP)
        _GDIPlus_Shutdown()
        GUIDelete($g_hGUI)
        Exit
    EndFunc   ;==>_Exit