Jump to content

How to Apply Filter to Image


Recommended Posts

Hopefully this is not a lame newb question. Actually I hope it is and 10 of you come running at me telling me what a jerk I am for asking this;

I load any image using GUICtrlCreatePic ("wallpaper\" & $pic,0,0,$dtx,$dty) for example this image.

Posted Image

That works out just fine, but I want to apply a filter at the top. Such as this;

Posted Image

or even a gradient? Like this;

Posted Image

I tried a few options like that Alphablend example but whoa! That's a lot more than I want and would require loading another image. If I wanted that I could just go through all my wallpaper and apply a filter. I want this to be done in AutoIt and not mess with the saved image.

I am thinking there must be a simple command or a few commands I am overlooking in the help file. Maybe a way to set a big black label that is 50% transparent? I don't know so that is what I am asking.

JAB

Link to comment
Share on other sites

This method uses two GUI's with the top GUI partially transparent.

#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>

Global $PIGui, $Gui, $GPos, $PPos, $iTrans
Local $width = 600, $height = 400, $left = 300, $top = 300

$PIGui = GUICreate("My GUI picture", $width + 40, $height + 40, $left, $top, $WS_POPUPWINDOW)
GUISetBkColor(0xE0FFFF)

Local $Pic = GUICtrlCreatePic("utahnationalpark.jpg", 20, 20, 600, 399)
GUICtrlSetState(-1, $GUI_DISABLE)
Local $slider1 = GUICtrlCreateSlider(40, $height + 22, $width / 3, 16)
GUICtrlSetLimit(-1, 255, 0)
GUICtrlSetData($slider1, 127)
Local $label = GUICtrlCreateLabel("127", 15, $height + 22, 25, 16, 0x1002)
GUICtrlSetBkColor(-1, 0xFFFFa0)
GUISetState()

$Gui = GUICreate("Test", $width, $height / 2, $left + 20, $top + 20, $WS_POPUPWINDOW, $WS_EX_TOPMOST)
GUISetBkColor(0x000000)
WinSetTrans("Test", "", 127)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3 ; $GUI_EVENT_CLOSE
            Exit
        Case $slider1
            $iTrans = GUICtrlRead($slider1)
            WinSetTrans("Test", "", $iTrans)
            GUICtrlSetData($label, $iTrans)
    EndSwitch
WEnd
Link to comment
Share on other sites

Malkey, I've made some changes to your script and added some gradient:

#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>

_Main()

Func _Main()
 Local $hMainGUI, $hTopGUI, $hPic, $width = 600, $height = 400, $left = (@DesktopWidth-600)/2, $top = (@DesktopHeight-400)/2

 $hTopGUI = GUICreate("Test", 600, 200, $left+1, $top+1, $WS_POPUP, $WS_EX_TOPMOST)
 _Gradient(0x000000, 0xDEDEDE, 600, 200, 0, 0,1)
 WinSetTrans($hTopGUI,"",0)
 GUISetState()

 $hMainGUI = GUICreate("My GUI picture", $width, $height+40, $left, $top, $WS_POPUP)
 $hPic = GUICtrlCreatePic("utahnationalpark.jpg", 0, 0, 600, 400)
 GUICtrlSetState(-1, $GUI_DISABLE)
 $hSlider = GUICtrlCreateSlider(40, $height +12, $width / 3, 16)
 GUICtrlSetLimit(-1, 254, 0)
 GUICtrlSetData($hSlider, 127)
 GUISetState()

 While 1
  Sleep(10)
  WinSetTrans($hTopGUI, "", GUICtrlRead($hSlider))
  Switch GUIGetMsg()
   Case -3
    Exit
  EndSwitch
 WEnd
EndFunc

; #FUNCTION# ====================================================================================================
; Name...........:  _Gradient
; Description....:  Create gradient between two colors
; Syntax.........:  _Gradient($lColor1, $lColor2, $iW=100, $iH=20, $iX=0, $iY=0, $iDirection=0)
; Parameters.....:  $lColor1 - First color to begin with
;                   $lColor2 - Second color
;                   $iW - width of the area, [Optional]
;                   $iH - height of the area, [Optional]
;                   $iX - position from left, [Optional]
;                   $iY - position from top, [Optional]
;                   $iDirection - direction to fill the gradient, [Optional]
;                     1 - vertical fill
;                     0 - horizontal fill
; Return values..:  none
; Author.........:  taietel
; ===============================================================================================================
Func _Gradient($lColor1, $lColor2, $iW=100, $iH=20, $iX=0, $iY=0, $iDirection=0)
    Local $m, $Ri,$Rf,$Rs,$Gi,$Gf,$Gs,$Bi,$Bf,$Bs
    Switch $iDirection
        Case 0
            $m = $iW
        Case 1
            $m = $iH
    EndSwitch

    $Ri = Mod($lColor1,256)
    $Gi = BitAND($lColor1/256,255)
    $Bi = BitAND($lColor1/65536,255)

    $Rf = Mod($lColor2,256)
    $Gf = BitAND($lColor2/256,255)
    $Bf = BitAND($lColor2/65536,255)

    $Rs = Abs($Ri - $Rf)/$m
    $Gs = Abs($Gi - $Gf)/$m
    $Bs = Abs($Bi - $Bf)/$m

    If $Rf < $Ri Then $Rs = -$Rs
    If $Gf < $Gi Then $Gs = -$Gs
    If $Bf < $Bi Then $Bs = -$Bs

    For $i = 0 To $m
        $Rf = $Ri + $Rs * $i
        $Gf = $Gi + $Gs * $i
        $Bf = $Bi + $Bs * $i
        Local $hLbl = GUICtrlCreateLabel(" ", $iX+$i-$i*$iDirection, $iY + $i*$iDirection, 2+$iDirection*$iW, 2+(1-$iDirection)*$iH)
        GUICtrlSetBkColor($hLbl, "0x"&Hex($Bf,2) & Hex($Gf,2) & Hex($Rf,2))
    Next
EndFunc

[EDIT] Corrected flickering.

Edited by taietel
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...