Jump to content

Image fading to transparent


BigOneWay
 Share

Recommended Posts

Goodmorning everyone
Houston have a problem, or rather, I have a problem, I'm trying to turn a bitmap image into an image gradient towards transparency, the code below uses two PNG images I attach, the first is the source image, the second is how it should become.
I thought I had found the solution here: https://www.autoitscript.com/forum/topic/152668-gdi-rotate-trasparency-gradient/

But the result was not the right one.
I tried to figure out if you could use GDIPlus's FX, WinAPI_AlphaBlend, _WinAPI_GradientFill, _GDIPlus_PathTranslate, and so many others, but it's too much for me.
You are my last resource, if you do not know it, it means that it is impossible

#include <WindowsConstants.au3>
#include <GDIPlus.au3>

Global $width = 220
Global $height = 150

$X = (@DesktopWidth - $width)/2
$Y = (@DesktopHeight - $height)/2

Global $graphics, $backbuffer, $bitmap, $Pen, $arrTxt1, $arrTxt2, $fontsize_txt1, $fontsize_txt2

Global $ScreenDc, $dc, $tSize, $pSize, $tSource, $pSource, $tBlend, $pBlend, $tPoint, $pPoint, $gdibitmap

$hwnd = GUICreate('', $width, $height, $X, $Y, 0, $WS_EX_LAYERED)

_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $graphics)
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
_GDIPlus_GraphicsSetSmoothingMode($backbuffer, 2)

$ScreenDc = _WinAPI_GetDC($hWnd)
$gdibitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($bitmap)
$dc = _WinAPI_CreateCompatibleDC($ScreenDc)
_WinAPI_SelectObject($dc, $gdibitmap)
    ; _WinAPI_UpdateLayeredWindow parameters
$tSize = DllStructCreate($tagSIZE)
$pSize = DllStructGetPtr($tSize)
DllStructSetData($tSize, "X", $width)
DllStructSetData($tSize, "Y", $height)
$tSource = DllStructCreate($tagPOINT)
$pSource = DllStructGetPtr($tSource)
$tBlend = DllStructCreate($tagBLENDFUNCTION)
$pBlend = DllStructGetPtr($tBlend)
Global $alpha = 255
Global $alpha_steps = 5
DllStructSetData($tBlend, "Alpha", $alpha)
DllStructSetData($tBlend, "Format", 1)
$tPoint = DllStructCreate($tagPOINT)
$pPoint = DllStructGetPtr($tPoint)
DllStructSetData($tPoint, "X", 0)
DllStructSetData($tPoint, "Y", 0)

GUISetState()

    $hOmage = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\Autoit.png')
    $hOmage = _GDIPlus_ImageResize($hOmage, 70, 70)

    $hOmage1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\Autoit1.png')
    $hOmage1 = _GDIPlus_ImageResize($hOmage1, 70, 70)

    _GDIPlus_GraphicsDrawImage ($backbuffer, $hOmage, 15, 70)
    _GDIPlus_GraphicsDrawImage ($backbuffer, $hOmage1, 85, 70)

    $gdibitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($bitmap)
    _WinAPI_SelectObject($dc, $gdibitmap)
    _WinAPI_UpdateLayeredWindow($hWnd, $ScreenDc, 0, $pSize, $dc, $pSource, 0, $pBlend, 2)
    _WinAPI_DeleteObject($gdibitmap)


while 1
    Sleep(100)
wend

I thank everyone who wants to help me

AutoitPNG.zip

Link to comment
Share on other sites

  • Developers
1 hour ago, BigOneWay said:

Houston have a problem

Is this referring to the recent Harvey flooding in Houston or did you mean the Apollo 13 astronaut stating "Houston, we have a problem" ?
Either way, please use the standard text format in stead of using bold on all your text typed. ;)

Jos

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@BigOneWay: here a fast hack for what you probably looking for:

;coded by UEZ build 2017-09-15
#include <GDIPlus.au3>

_GDIPlus_Startup()

Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\AutoIt.png")
Global $hBitmap_Result = _GDIPlus_BitmapCreateTransparentFading($hImage)

_GDIPlus_ImageSaveToFile($hBitmap_Result, @ScriptDir & "\AutoIt_faded.png")

_GDIPlus_ImageDispose($hBitmap_Result)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()


Func _GDIPlus_BitmapCreateTransparentFading($hBitmap, $iDir = 1)
    $iDir = $iDir > 4 ? 4 : $iDir < 1 ? 1 : $iDir
    Local Const $iW = _GDIPlus_ImageGetWidth($hBitmap), $iH = _GDIPlus_ImageGetHeight($hBitmap)
    Local Const $hBitmap_Result = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
    Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap_Result, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32ARGB)
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
    Local $tPixel = DllStructCreate("uint[" & $iW * $iH & "];", $iScan0)
    Local $iRGB, $iRowOffset, $iAlpha, $iAlpha_new
    Switch $iDir
        Case 1 ;left to right
            For $iY = 0 To $iH - 1
                $iRowOffset = $iY * $iW + 1
                For $iX = 0 To $iW - 1
                    $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
                    $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
                    $iAlpha_new = 255 / $iW * $iX
                    $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
                    DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
                Next
            Next
    EndSwitch
    _GDIPlus_BitmapUnlockBits($hBitmap_Result, $tBitmapData)

    Return $hBitmap_Result
EndFunc

 

Homework for you: implement the other 3 directions.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

sorry for the delay, Sensei.
put wax, removed wax, like 'Karate Kid'.

between work and the other, I did this.

Case 2 ;right to left
            For $iY = 0 To $iH - 1
                $iRowOffset = $iY * $iW + 1
                For $iX = $iW - 1 To 0 step - 1
                    $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iW - 1 - $iX), 0x00FFFFFF)
                    $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iW - 1 - $iX), 24), 0xFF)
                    $iAlpha_new = 255 / $iW * $iX
                    $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
                    DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iW - 1 - $iX)
                Next
            Next
        Case 3 ;down to up
;~          For $iX = 0 To $iW - 1
;~              For $iY = 0 To $iH - 1
;~                  $iRowOffset = $iY * $iW + 1
;~                  $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
;~                  $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
;~                  $iAlpha_new = 255 / $iH * $iY
;~                  $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
;~                  DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
;~              Next
;~          Next
;Or
            For $iY = 0 To $iH - 1
                $iRowOffset = $iY * $iW + 1
                For $iX = 0 To $iW - 1
                    $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
                    $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
                    $iAlpha_new = 255 / $iH * $iY
                    $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
                    DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
                Next
            Next
        Case 4 ;up to down
            For $iX = 0 To $iW - 1
                For $iY = $iH - 1 To 0 step - 1
                    $iRowOffset = $iY * $iW + 1
                    $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
                    $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
                    $iAlpha_new = 255 / $iH * $iY
                    $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
                    DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
                Next
            Next

I miss Up to Down.


Now I come home and after the work of my father and husband complete my mission. :sweating:

Anyway, I knew you had the answer. o:)

Thank you

Link to comment
Share on other sites

Completed mission, sensei, now I can walk head high, toward sunset on the horizon.

Yep, yep, the important thing is to believe it

Please do not ask me if I really understood it, I would say a lie.
but it works, and I had no doubts.

Case 1 ;left to right
For $iY = 0 To $iH - 1
$iRowOffset = $iY * $iW + 1
For $iX = 0 To $iW - 1
$iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
$iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
$iAlpha_new = 255 / $iW * $iX
$iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
Next
Next
Case 2 ;right to left
For $iY = 0 To $iH - 1
$iRowOffset = $iY * $iW + 1
For $iX = 0 To $iW - 1
$iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
$iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
$iAlpha_new = 255 - 255 / $iW * $iX
$iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
Next
Next
Case 3 ;down to up
For $iY = 0 To $iH - 1
$iRowOffset = $iY * $iW + 1
For $iX = 0 To $iW - 1
$iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
$iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
$iAlpha_new = 255 / $iH * $iY
$iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
Next
Next
Case 4 ;up to down
For $iY = 0 To $iH - 1
$iRowOffset = $iY * $iW + 1
For $iX = 0 To $iW - 1
$iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
$iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
$iAlpha_new = 255 - 255 / $iH * $iY
$iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
Next
Next

P.S. Sorry, but now I'm using a proxy that does not allow me to use the tag code

Another huge wall was broken to make another small step.
Your advice is always precious.
Thanks, Marco

Link to comment
Share on other sites

A modified UEZ example without a Switch statement.

;coded by UEZ build 2017-09-15
#include <GDIPlus.au3>
; https://www.autoitscript.com/forum/topic/190373-image-fading-to-transparent/

For $j = 1 To 4
    _GDIPlus_Startup()

    Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\AutoIt.png")
    Global $hBitmap_Result = _GDIPlus_BitmapCreateTransparentFading($hImage, $j)

    _GDIPlus_ImageSaveToFile($hBitmap_Result, @ScriptDir & "\AutoIt_faded.png")

    _GDIPlus_ImageDispose($hBitmap_Result)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    ShellExecute(@ScriptDir & "\AutoIt_faded.png")
Next

; Parameter
; $iDir = 1 - Fade from right to left (left faded),
;         2 - Fade from left to right (right faded),
;         3 - Fade from top to botom  (botom faded),
;         4 - Fade from bottom to top (top faded).
Func _GDIPlus_BitmapCreateTransparentFading($hBitmap, $iDir = 1)
    $iDir = ($iDir > 4 or $iDir < 1) ?  1 : $iDir
    Local $aAlpha_newFormula[5] = ["", "255 / $iW * $iX", "255 / $iW * ($iW - 1 - $iX)", "255 / $iH * ($iH - 1) - $iY", "255 / $iH * $iY"]
    Local Const $iW = _GDIPlus_ImageGetWidth($hBitmap), $iH = _GDIPlus_ImageGetHeight($hBitmap)
    Local Const $hBitmap_Result = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
    Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap_Result, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32ARGB)
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
    Local $tPixel = DllStructCreate("uint[" & $iW * $iH & "];", $iScan0)
    Local $iRGB, $iRowOffset, $iAlpha, $iAlpha_new

    For $iY = 0 To $iH - 1
        $iRowOffset = $iY * $iW + 1
        For $iX = 0 To $iW - 1
            $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
            $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
            $iAlpha_new = Execute($aAlpha_newFormula[$iDir])
            $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
            DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hBitmap_Result, $tBitmapData)

    Return $hBitmap_Result
EndFunc   ;==>_GDIPlus_BitmapCreateTransparentFading

 

Link to comment
Share on other sites

@BigOneWay good work trainee :thumbsup: You earned the white belt in GDI+. :ninja:

 

@Malkey good approach to shorten the code. I got a similar idea by saving only the parameters for the 2 For/Next loops or using _GDIPlus_ImageRotateFlip accordingly. But the learning effect is better with the long code. :D

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Here the idea using  _GDIPlus_ImageRotateFlip():

;coded by UEZ build 2017-09-16
#include <GDIPlus.au3>

_GDIPlus_Startup()
Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\AutoIt.png")
Global $hBitmap_Result = _GDIPlus_BitmapCreateTransparentFading($hImage)

_GDIPlus_ImageSaveToFile($hBitmap_Result, @ScriptDir & "\AutoIt_faded.png")

_GDIPlus_ImageDispose($hBitmap_Result)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
ShellExecute(@ScriptDir & "\AutoIt_faded.png")

Func _GDIPlus_BitmapCreateTransparentFading($hBitmap, $iDir = 1)
    $iDir = $iDir > 4 ? 4 : $iDir < 1 ? 1 : $iDir
    Local $iW = _GDIPlus_ImageGetWidth($hBitmap), $iH = _GDIPlus_ImageGetHeight($hBitmap)
    Local Const $hBitmap_Result = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
    Local $t
    Switch $iDir ;rotate image
        Case 2 ;right to left
            _GDIPlus_ImageRotateFlip($hBitmap_Result, 2)
        Case 3, 4
            If $iDir = 3 Then
                _GDIPlus_ImageRotateFlip($hBitmap_Result, 3) ;up to down
            Else
                _GDIPlus_ImageRotateFlip($hBitmap_Result, 7) ;down to up
            EndIf
            $t = $iW
            $iW = $iH
            $iH = $t
    EndSwitch

    Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap_Result, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32ARGB)
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0")
    Local $tPixel = DllStructCreate("uint[" & $iW * $iH & "];", $iScan0)
    Local $iRGB, $iRowOffset, $iAlpha, $iAlpha_new

    For $iY = 0 To $iH - 1 ;left to right by default
        $iRowOffset = $iY * $iW + 1
        For $iX = 0 To $iW - 1
            $iRGB = BitAND(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 0x00FFFFFF)
            $iAlpha = BitAND(BitShift(DllStructGetData($tPixel, 1, $iRowOffset + $iX), 24), 0xFF)
            $iAlpha_new = 255 / $iW * $iX
            $iAlpha_new = $iAlpha_new > $iAlpha ? $iAlpha : $iAlpha_new
            DllStructSetData($tPixel, 1, BitShift($iAlpha_new, -24) + $iRGB, $iRowOffset + $iX)
        Next
    Next

    _GDIPlus_BitmapUnlockBits($hBitmap_Result, $tBitmapData) ;unlocks a portion of a bitmap that was locked by _GDIPlus_BitmapLockBits

    Switch $iDir  ;rotate image back
        Case 2 ;right to left
            _GDIPlus_ImageRotateFlip($hBitmap_Result, 2)
        Case 3 ;up to down
            _GDIPlus_ImageRotateFlip($hBitmap_Result, 1)
        Case 4 ;down to up
            _GDIPlus_ImageRotateFlip($hBitmap_Result, 7)
    EndSwitch

    Return $hBitmap_Result
EndFunc   ;==>_GDIPlus_BitmapCreateTransparentFading

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Only now I have reset the browser, dns problems.
I thank you again for the time you have dedicated me, I have a small project for the head but I can not tell you, you would do it in 5 minutes and I would have nothing to do anymore.

For me, for the moment ........., that's enough

Thanks again.

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...