Sign in to follow this  
Followers 0
Malkey

Bevelled Edge Effect on Image

3 posts in this topic

#1 ·  Posted (edited)

Nahuel, I hope you do not mind my using your bitmap.bmp image file from

http://www.autoitscript.com/forum/index.ph...st&p=582541

Thanks Martin for sharing the great idea and example on how to distort an image at

http://www.autoitscript.com/forum/index.ph...st&p=590018

The two functions, GDIPlus_TopBotFade() and GDIPlus_AddBevel() work on the same principle. You specify a distance from an edge (top or bottom), or border width.

From that distance to the edge the transparency increases linearly until, at the edge of the image, there is total transparency. The chosen set colours behind the image gradually appear with the reducing opaqueness of the image.

The GDIPlus_ prefix is used because _GDIPlus_Startup() has to be called before, and _GDIPlus_Shutdown() has to be called after using these functions.

The GDIPlus_TopBotFade() function uses a similar technique to the perspective method by Martin - One line at a time.

Both function need _GDIPlus_GraphicsDrawImageRectRectTrans() originally posted here

http://www.autoitscript.com/forum/index.ph...st&p=586088

and GDIPlus_GraphicsFillPolygon(). Both of these are in the example script.

This image file should auto-download when the example is run the first time, and save it to the desktop as bitmap.png by using the InetGet() function in the script..

post-31256-1224504936_thumb.png

Enjoy.

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

Global $width = 700
Global $height = 300
Global $iTrans
$hwnd = GUICreate("test", $width, $height)
GUISetState()
If Not FileExists(@DesktopDir & "bitmap.png") Then InetGet("http://www.autoitscript.com/forum/uploads/monthly_10_2008/post-31256-1224504936_thumb.png", _
                                                                                @DesktopDir & "bitmap.png")
_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
_GDIPlus_GraphicsClear($graphics, 0xFF008800)

$image = _GDIPlus_ImageLoadFromFile(@DesktopDir & "bitmap.png")
$iwidth = _GDIPlus_ImageGetWidth($image)
$iheight = _GDIPlus_ImageGetHeight($image)
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hwnd)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)

$brush = _GDIPlus_BrushCreateSolid(0xFF000000)
$family = _GDIPlus_FontFamilyCreate("Arial")
$font = _GDIPlus_FontCreate($family, 24)
$layout = _GDIPlus_RectFCreate(0, 250, $width, $height)
$format = _GDIPlus_StringFormatCreate()

_GDIPlus_StringFormatSetAlign($format, 2)
_GDIPlus_GraphicsDrawStringEx($graphics, "Normal   ", $font, $layout, $format, $brush)
_GDIPlus_GraphicsDrawImageRect($graphics, $image, 480, 20, $iwidth, $iheight)

_GDIPlus_StringFormatSetAlign($format, 1)
_GDIPlus_GraphicsDrawStringEx($graphics, "Top and Bottom Fade ", $font, $layout, $format, $brush)
GDIPlus_TopBotFade($hGraphic, $image, $iheight / 2 - 20, 30, 0xFF666666, 0xFF333333)
_GDIPlus_GraphicsDrawImage($graphics, $hBMPBuff, 250, 20)

_GDIPlus_StringFormatSetAlign($format, 0)
_GDIPlus_GraphicsDrawStringEx($graphics, "  Bevelled", $font, $layout, $format, $brush)
GDIPlus_AddBevel($graphics, $image, 30, 0xFF606060, 0xFFAFAFAF)
_GDIPlus_GraphicsDrawImage($graphics, $hBMPBuff, 20, 20)

Do
    sleep(10)
Until GUIGetMsg() = -3

_GDIPlus_ImageDispose($image)
_GDIPlus_BrushDispose($brush)
_GDIPlus_GraphicsDispose($graphics)
_GDIPlus_GraphicsDispose($hGraphic)
_WinAPI_DeleteObject($hBMPBuff)
_GDIPlus_FontDispose($font)
_GDIPlus_FontFamilyDispose($family)
_GDIPlus_StringFormatDispose($format)
_GDIPlus_Shutdown()


Func _GDIPlus_StringFormatSetAlignOld($hStringFormat, $iFlag)
    Local $aResult
    $aResult = DllCall($ghGDIPDll, "int", "GdipSetStringFormatAlign", "ptr", $hStringFormat, "short", $iFlag)
    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] = 0 Then Return SetError(0, 0, 1)
    Return SetError(1, 0, 0)
EndFunc   ;==>_GDIPlus_StringFormatSetAlign

Func GDIPlus_TopBotFade($hGraphic, $image, $topless, $Botless, $TopColor, $iBotColor, $TotTrans = 1)
    Local $iwidth = _GDIPlus_ImageGetWidth($image)
    Local $iheight = _GDIPlus_ImageGetHeight($image)
    Local $aPoints[5][2] = [[4, 0],[0, 0],[$iwidth, 0],[$iwidth, $topless],[0, $topless]]
    GDIPlus_GraphicsFillPolygon($hGraphic, $aPoints, $TopColor)
    Local $aPoints[5][2] = [[4, 0],[0, $iheight - $Botless],[$iwidth, $iheight - $Botless],[$iwidth, $iheight],[0, $iheight]]
    GDIPlus_GraphicsFillPolygon($hGraphic, $aPoints, $iBotColor)
    For $n = 0 To $iheight - 1
        If ($iheight - $n) <= $Botless Then ; Bottom invisible up $topless
            $iTrans = (($iheight - $n) / $Botless)
        ElseIf $n <= $topless Then ; Top invisible down $topless
            $iTrans = $n / $topless
        Else
            $iTrans = $TotTrans
        EndIf
        If $iTrans > $TotTrans Then $iTrans = $TotTrans
        _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphic, $image, 1, $n, $iwidth, 1, 1, $n, $iwidth, 1, 2, $iTrans)
    Next
    Return
EndFunc   ;==>GDIPlus_TopBotFade

Func GDIPlus_AddBevel($graphics, $image, $iBdr, $iTopLeftColor, $iBotRightColor)
    Local $iwidth = _GDIPlus_ImageGetWidth($image)
    Local $iheight = _GDIPlus_ImageGetHeight($image)
    Local $aPoints[4][2] = [[3, 0],[0, 0],[$iwidth, 0],[0, $iheight]]
    GDIPlus_GraphicsFillPolygon($hGraphic, $aPoints, $iTopLeftColor)
    Local $aPoints[4][2] = [[3, 0],[$iwidth, 0],[$iwidth, $iheight],[0, $iheight]]
    GDIPlus_GraphicsFillPolygon($hGraphic, $aPoints, $iBotRightColor)
    For $n = 0 To $iBdr
        $iTrans = Round($n / $iBdr, 3)
        ;ConsoleWrite(" $iTrans  = " & $iTrans & "   $n = "& $n  & @CRLF)
        _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphic, $image, $n, $n, $iwidth - 2 * $n, $iheight - 2 * $n, $n, $n, $iwidth - 2 * $n, $iheight - 2 * $n, 2, $iTrans)
    Next
    Return
EndFunc   ;==>GDIPlus_AddBevel

Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _
        $iDstX = "", $iDstY = "", $iDstWidth = "", $iDstHeight = "", $iUnit = 2, $nTrans = 1)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
    If $iSrcWidth = 0 Or $iSrcWidth = "" Then $iSrcWidth = $iW
    If $iSrcHeight = 0 Or $iSrcHeight = "" Then $iSrcHeight = $iH
    If $iDstX = "" Then $iDstX = $iSrcX
    If $iDstY = "" Then $iDstY = $iSrcY
    If $iDstWidth = "" Then $iDstWidth = $iSrcWidth
    If $iDstHeight = "" Then $iDstHeight = $iSrcHeight
    If $iUnit = "" Then $iUnit = 2
    ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;blending values:
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 2, 1, 2) * DllStructSetData($tColorMatrix, 3, 1, 3) * _
            DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 5)
    ;;create an image attributes object and update its color matrix
    $hImgAttrib = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    $hImgAttrib = $hImgAttrib[1]
    DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _
            "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0)
    ;;draw image into graphic object with alpha blend
    DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
            $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
            $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    ;;clean up
    DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectTrans

Func GDIPlus_GraphicsFillPolygon($hGraphics, $aPoints, $iARGB = -1)
    Local $iI, $iCount, $pPoints, $tPoints, $aResult, $tmpError, $tmpExError
    $iCount = $aPoints[0][0]
    $tPoints = DllStructCreate("int[" & $iCount * 2 & "]")
    $pPoints = DllStructGetPtr($tPoints)
    For $iI = 1 To $iCount
        DllStructSetData($tPoints, 1, $aPoints[$iI][0], (($iI - 1) * 2) + 1)
        DllStructSetData($tPoints, 1, $aPoints[$iI][1], (($iI - 1) * 2) + 2)
    Next
    $hBrush = _GDIPlus_BrushCreateSolid($iARGB)
    $aResult = DllCall($ghGDIPDll, "int", "GdipFillPolygonI", "hWnd", $hGraphics, "hWnd", $hBrush, _
            "ptr", $pPoints, "int", $iCount, "int", "FillModeAlternate")
    $tmpError = @error
    $tmpExError = @extended
    _GDIPlus_BrushDispose($hBrush)
    If $tmpError Then Return SetError($tmpError, $tmpExError, False)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>GDIPlus_GraphicsFillPolygon

Edit: As "Func _GDIPlus_StringFormatSetAlign(" already exists as a UDF, changed to "Func _GDIPlus_StringFormatSetAlignOld(".

Edited by Malkey

Share this post


Link to post
Share on other sites



Very well done!


[quote name='PsaltyDS' post='635433' date='Jan 27 2009, 07:04 AM']Larry is a mass murderer?! It's always the quiet, clean cut, bald guys... [/quote]

Share this post


Link to post
Share on other sites

Very good example Malkey. It is good for us to have you around. :P:(


When the words fail... music speaks

Share this post


Link to post
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
Sign in to follow this  
Followers 0