#include-once

; #FUNCTION# ====================================================================================================================
; Name...........:		_GUIImageList_AddBitmapEx
; Description ...:	Creates a bitmap from an image file (JPG, PNG, BMP, GIF, ICO, etc.) and adds it to the _GUIImageList control
; Syntax.........:		_GUIImageList_AddBitmapEx($hWnd, $sFile, $iWidth, $iHeight)
; Parameters ....:	$hWnd		- file name where the icon should be extracted from (*.dll, *.ico, *.exe)
;							$sFile			- path to the bitmap that contains the image
;                  			$iWidth		- width of the _GUIImageList
;							$iHeight		- height of the _GUIImageList
;							$iIndex = 0	- icon index which should be extracted
; Return values .:	Success		- Returns 1
;                 			Failure		- @error
; Author ........:		UEZ
; Version .......:		0.80 Build 2011-11-05 beta
; Remarks .......:	<GDIPlus.au3> must be included and _GDIPlus_Startup() must be started to use GDI+ functions.
;							Don't forget the _GDIPlus_Shutdown() when closing!
; ===============================================================================================================================
Func _GUIImageList_AddBitmapEx($hWnd, $sFile, $iWidth = 16, $iHeight = 16, $iIndex = 0)
	If Not IsHWnd($hWnd) = 0 Or $sFile = "" Or Not FileExists($sFile) Or IsDeclared("ghGDIPDll") <> 1 Then Return SetError(1, 0, 0)

	Local $ext = StringRight($sFile, 3)
	If $ext = "ico" Or  $ext = "exe" Or $ext = "dll" Then
		Local $aRet = DllCall("Shell32", "long", "ExtractAssociatedIcon", "int", 0, "str", $sFile, "word*", $iIndex)
		If @error Then Return SetError(1, @extended, 0)
		Local $hIcon = $aRet[0]
		Local $hDC = _WinAPI_GetDC(0) ;thanks to Yashied
		Local $hBackDC = _WinAPI_CreateCompatibleDC($hDC)
		Local $hBitmap = _WinAPI_CreateSolidBitmap(0, _WinAPI_GetSysColor($COLOR_MENU), $iWidth, $iHeight)
		Local $hBackSv = _WinAPI_SelectObject($hBackDC, $hBitmap)
        _WinAPI_DrawIconEx($hBackDC, 0, 0, $hIcon, $iWidth, $iHeight, 0, 0, 3)
		_WinAPI_SelectObject($hBackDC, $hBackSv)
		_WinAPI_ReleaseDC(0, $hDC)
		_WinAPI_DeleteDC($hBackDC)
		_GUIImageList_Add($hWnd, $hBitmap)
		_WinAPI_DeleteObject($hBitmap)
	Else
		Local $aProcessIcons = _GDIPlus_BitmapCreateFromFile($sFile)
		If @error Then Return SetError(2, @extended, 0)
		Local $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0
		Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
		If @error Then Return SetError(3, @extended, 0)

		Local $hClone = $aResult[6]
		Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hClone)
;~ 		DllCall($ghGDIPDll, "int", "GdipSetInterpolationMode", "handle", $hGraphics, "int", 5)
		_GDIPlus_GraphicsDrawImageRect($hGraphics, $aProcessIcons, 0, 0, $iWidth, $iHeight)

		Local $hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hClone)
		_GUIImageList_Add($hWnd, $hBmp)

		_GDIPlus_GraphicsDispose($hGraphics)
		_GDIPlus_BitmapDispose($hClone)
		_WinAPI_DeleteObject($hBmp)
		_GDIPlus_BitmapDispose($aProcessIcons)
	EndIf
	Return 1
EndFunc