Jump to content

GDIPlus GraphicsDrawImage in a circle


Recommended Posts

I want to create circle in GDI and yes in theory i can use PNG with an alfa channel but just for know if possible to load a circular jpg-bmp without edit-re save it?

This is what i'm using now, stripped

_GDIPlus_Startup()

    $hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Test.png")
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 100, 100, 100, 100)

    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)

    _GDIPlus_Shutdown()

The circle in theory will be bound the square of the image. Thanks for any help

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

I want to create circle in GDI and yes in theory i can use PNG with an alfa channel but just for know if possible to load a circular jpg-bmp without edit-re save it?

​I don't understand what you mean exactly. Can you describe it with an example please?

Thx.

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

Sure. With  _GDIPlus_GraphicsDrawImageRect i can take whatever image in whatever size and load it in a rectangle of a size of my choice, also if not corrisponding to original size, like this:

jtv2j4.jpg

Now, instead to draw in a rectangle, i'd like to use the same parameter of the above function ( example resize the original image to a square of 300 w and 300 h ) but draw it a circle ( circle inscribed in a square/rectangle formula ), in this way:

233d69.jpg

I have think to use something like load image as graphic handle  ( _GDIPlus_BitmapCreateFromFile+GDIPlus_ImageGetGraphicsContext ) and the use _GDIPlus_GraphicsFillEllipse. My attempts have failed but probably is my fault. I don't want to edit the original image or save it later on the disk, only display in the GUI. Thanks UEZ

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  1. load the image
  2. create a texture brush object from the bitmap -> _GDIPlus_TextureCreate
  3. draw a filled ellipse using the texture as the brush

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

I have already tried that method, works, but the problem is it can't load the entire image but only a portion of it, instead of DrawImageRect can load everything in a fixed size whatever the size of the image is

EDIT: Is a cool effect but not what i want :D

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

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Test.jpg") ;create an image object based on a file
Local $hGUI = GUICreate("GDI+ Example", 320, 320)
GUISetState(@SW_SHOW)

Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a Graphics object from a window handle
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2) ; $GDIP_SMOOTHINGMODE_HIGHQUALITY ;sets the graphics object rendering quality (antialiasing)

Local $hTexture = _GDIPlus_TextureCreate($hImage)
Do
    _GDIPlus_GraphicsFillEllipse($hGraphics, Random(1, 200), Random(1, 200), Random(1, 200), Random(1, 200), $hTexture) ;draw ellipse with texture as a brush
    Sleep(200)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;cleanup resources
_GDIPlus_BrushDispose($hTexture)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hGUI)

; 3.3.8.1
Func _GDIPlus_TextureCreate($hImage, $iWrapMode = 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateTexture", "handle", $hImage, "int", $iWrapMode, "handle*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] Then Return SetError(10, $aResult[0], 0)
    Return $aResult[3]
EndFunc   ;==>_GDIPlus_TextureCreate

See also this:

http://stackoverflow.com/questions/26845921/draw-image-into-shape

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

UEZ,

After many many ( many ) hours of research from the post of stackoverflow i have found the solution:

_GDIPlus_GraphicsSetClipPath

That, in combination with PathCreate+PathAddEllipse+GraphicsDrawImage, create an image like that one of my post. There is a sort of example in the help so is useless to post "mine" because is just a copy-paste of that. Many thanks for your help ( and point me on the wrong way lol ) but from that way i have found the solution! :D

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

I did this meanwhile:

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

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Test.jpg") ;create an image object based on a file
Local $hGUI = GUICreate("GDI+ Example", 320, 320)
GUISetState(@SW_SHOW)

Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a Graphics object from a window handle
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2) ; $GDIP_SMOOTHINGMODE_HIGHQUALITY ;sets the graphics object rendering quality (antialiasing)

Local $hBitmap = _GDIPlus_BitmapCreateEllipseFilled($hImage)



Do
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap,  Random(1, 200), Random(1, 200), Random(1, 200), Random(1, 200)) ;draw ellipse with texture as a brush
    Sleep(200)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;cleanup resources
_GDIPlus_ImageDispose($hImage)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hGUI)

Func _GDIPlus_BitmapCreateEllipseFilled($hBitmap, $iW = -1, $iH = -1, $iWrapMode = 0)
    If $iW = -1 Or $iH = -1 Then
        $iW = _GDIPlus_ImageGetWidth($hBitmap)
        $iH = _GDIPlus_ImageGetHeight($hBitmap)
    EndIf
    Local Const $hBmp = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hCtxt = _GDIPlus_ImageGetGraphicsContext($hBmp)
    Local Const $hTexture = _GDIPlus_TextureCreate($hBitmap, $iWrapMode)
    _GDIPlus_GraphicsSetInterpolationMode($hCtxt, 7)
    _GDIPlus_GraphicsSetCompositingQuality($hCtxt, 2)
    _GDIPlus_GraphicsFillEllipse($hCtxt, 0, 0, $iW, $iH, $hTexture)
    _GDIPlus_BrushDispose($hTexture)
    _GDIPlus_GraphicsDispose($hCtxt)
    Return $hBmp
EndFunc

 

Edited by UEZ
forgot _GDIPlus_BitmapDispose($hBitmap)

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