#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <GDIPlus.au3>
Opt("TrayAutoPause", 0)
_Example()
Func _Example()
Local $sFile = FileOpenDialog("Выберите изображение", Null, "Изображения (*.bmp;*.png;*.jpg;*.gif;*.tif)")
If @error Then Exit
Local $hTimer = TimerInit()
Local $iColor = _GetDominantColor($sFile, 2)
Switch @error
Case 1
Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "Ошибка", "Не удалось создать ассоциативный массив")
Case 2
Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "Ошибка", "Не удалось загрузить изображение")
Case 3
Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "Ошибка", "Не удалось получить цвет")
EndSwitch
GUICreate(Round(TimerDiff($hTimer) / 1000, 2) & " сек. " & $iColor, 400, 400)
GUISetBkColor($iColor)
GUISetState()
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Example
Func _GetDominantColor($sFile, $iStep = 1)
Local $oDict = ObjCreate("Scripting.Dictionary")
If Not IsObj($oDict) Then Return SetError(1, 0, 0)
Local $fGDI = False
If Not $__g_hGDIPDll Then $fGDI = True
If $iStep = Default Or $iStep <= 0 Then $iStep = 1
_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
If @error Then Return SetError(2, $fGDI ? _GDIPlus_Shutdown() : 0, 0)
Local $iW = _GDIPlus_ImageGetWidth($hImage)
Local $iH = _GDIPlus_ImageGetHeight($hImage)
Local $tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)
Local $tPixel = DllStructCreate("INT[" & $iW * $iH & "]", DllStructGetData($tBitmapData, "Scan0"))
Local $iPixel, $iRowOffset
For $iY = 0 To $iH - 1 Step $iStep
$iRowOffset = $iY * $iW + 1
For $iX = 0 To $iW - 1 Step $iStep
$iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
Switch $iPixel
Case 0, -1, -16777216
ContinueLoop
EndSwitch
$oDict.Item($iPixel) += 1
Next
Next
_GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)
_GDIPlus_ImageDispose($hImage)
If $fGDI Then _GDIPlus_Shutdown()
Local $aKeys = $oDict.Keys()
Local $aItems = $oDict.Items()
Local $i = _ArrayMaxIndex($aItems, 1)
If @error Or Not IsArray($aKeys) Then Return SetError(3, 0, 0)
Return SetError(0, 0, "0x" & Hex($aKeys[$i], 6))
EndFunc ;==>_GetDominantColor