Jump to content

[SOLVED] GDI+ rounded corners on a image


nend
 Share

Recommended Posts

Hi Guys,

Is it possible with GDI+ to make transparent rounded corners on a image?
I found a few examples but that output a GUI with rounded corners but I couldn't find a example of a image.

This is what I have so far.

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

_GDIPlus_Startup()

Local Const $iWidth = 400, $iHeight = 200

Local $hGUI = GUICreate("", $iWidth, $iHeight)

GUISetState(@SW_SHOW)

Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
$hBrush = _GDIPlus_LineBrushCreate(17, 110, 320, 110, 0xFF000000, 0xFFFFFFFF , 1)
_GDIPlus_GraphicsFillRect($hGraphics, 20, 20, 297, 109, $hBrush)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()

Thanks for the help!

Edited by nend
Link to comment
Share on other sites

Here we go:

#include <GDIPlus.au3>

Global $sFile = FileOpenDialog("Select an image", "", "Images (*.jpg;*.bmp;*.png;*.gif)")
If @error Then Exit MsgBox(16, "Error", "You must select an image!", 20)

_GDIPlus_Startup()
Global Const $hImg = _GDIPlus_ImageLoadFromFile($sFile)
Global Const $hBmp = _GDIPlus_BitmapCreateRoundedCornerFromBitmap($hImg)
_GDIPlus_ImageSaveToFile($hBmp, @ScriptDir & "\TestImg.png")
_GDIPlus_ImageDispose($hImg)
_GDIPlus_ImageDispose($hBmp)
_GDIPlus_Shutdown()
ShellExecute(@ScriptDir & "\TestImg.png")

Func _GDIPlus_BitmapCreateRoundedCornerFromBitmap($hImage, $fRadiusCorner = -1, $bBorder = False, $iBorderColor = 0xE0FFFFFF, $iBorderSize = 1) ;coded by UEZ buid 2017-04-17
    Local $aDim = _GDIPlus_ImageGetDimension($hImage)
    If @error Then Return SetError(1, 0, 0)

    Local Const $iW = $aDim[0], $iH = $aDim[1]
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hGfx, 4)
    _GDIPlus_GraphicsSetPixelOffsetMode($hGfx, 4)
    _GDIPlus_GraphicsSetCompositingQuality($hGfx, 2)

    Local Const $hTexture = _GDIPlus_TextureCreate($hImage)
    Local Const $hPath = _GDIPlus_PathCreate()

    If $fRadiusCorner = -1 Then
        $fRadiusCorner = (($iW + $iH) / 2) * 0.1
    EndIf

    _GDIPlus_PathAddArc($hPath, $iW - ($fRadiusCorner * 2), 0, $fRadiusCorner * 2, $fRadiusCorner * 2, 270, 90)
    _GDIPlus_PathAddArc($hPath, $iW - ($fRadiusCorner * 2), $iH - ($fRadiusCorner * 2), $fRadiusCorner * 2, $fRadiusCorner * 2, 0, 90)
    _GDIPlus_PathAddArc($hPath, 0, $iH - ($fRadiusCorner * 2), $fRadiusCorner * 2, $fRadiusCorner * 2, 90, 90)
    _GDIPlus_PathAddArc($hPath, 0, 0, $fRadiusCorner * 2, $fRadiusCorner * 2, 180, 90)
    _GDIPlus_PathCloseFigure($hPath)

    _GDIPlus_GraphicsFillPath($hGfx, $hPath, $hTexture)
    If $bBorder Then
        Local Const $hPen = _GDIPlus_PenCreate($iBorderColor, $iBorderSize)
        _GDIPlus_PenSetLineJoin($hPen, 2)
        _GDIPlus_PenSetAlignment($hPen, 1)
        _GDIPlus_GraphicsDrawPath($hGfx, $hPath, $hPen)
        _GDIPlus_PenDispose($hPen)
    EndIf

    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_BrushDispose($hTexture)
    _GDIPlus_PathDispose($hPath)

    Return $hBitmap
EndFunc   ;==>_GDIPlus_BitmapCreateRoundedCornerFromBitmap

 

Edited by UEZ
added border functionality

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

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