Jump to content

How I get a pic into gdi manipulation


Bakiki
 Share

Recommended Posts

Hi

Firstly, I realise there in the _ScreenCapture_Capture command but I want to select a section from a jpg file... Then I want to manipuale it in the same enviroment as _GDIPlus_GraphicsFillEllipse. Yes, it's a very newbie question but all the examples scripts I find seem to work with one or the other but not both together... Here is what I've got so far...

Thanks

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>

Global $g_hGUI, $g_hGfxCtxt, $g_hBitmap, $g_hBMP, $g_hGraphics

Example()

Func Example()
    AutoItSetOption("GUIOnEventMode", 1)

    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBGColor format RRGGBB

    $g_hGUI = GUICreate("GDI+ example", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $g_hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    ;create buffered graphics frame set for smoother gfx object movements
    $g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($g_hGUI) ;create a graphics object from a window handle
    $g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $g_hGraphics)
    $g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap)
    Local $iW = 300, $iH = 300
    Local $hHBmp = _ScreenCapture_Capture("", 40, 40, $iW, $iH) ;create a GDI bitmap by capturing an area on desktop
    $g_hBrush = _GDIPlus_BrushCreateSolid(0xFF0000)

    $g_hBMP = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
    Local $iVectorX = Random(1.5, 2.5), $iVectorY = Random(1.5, 2.5) ;define x and y vector
    Local $iX = 0.0, $iY = 0.0 ;define start coordinate

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

    Do
        _GDIPlus_GraphicsClear($g_hGfxCtxt, 0xFF000000 + $iBgColor) ;clear bitmap for repaint
        _GDIPlus_GraphicsDrawImage($g_hGfxCtxt, $g_hBMP, $iX, $iY) ;draw bitmap to backbuffer
        _GDIPlus_GraphicsDrawImageRect($g_hGraphics, $g_hBitmap, 0, 0, $iWidth, $iHeight) ;copy drawn bitmap to graphics handle (GUI)
        $iX += $iVectorX ;add x vector to current x position
        $iY += $iVectorY ;add y vector to current y position
        If $iX < 0 Or $iX > ($iWidth - $iW) Then $iVectorX *= -1 ;when x border is reached reverse x vector
        If $iY < 0 Or $iY > ($iHeight - $iH) Then $iVectorY *= -1 ;when y border is reached reverse y vector

        ;this is what is not working #################
        _GDIPlus_GraphicsFillEllipse($g_hGfxCtxt, $iX+50, $iY+50, 30, 30, $g_hBrush)


    Until Not Sleep(10) ;sleep 10 ms to avoid high cpu usage
EndFunc   ;==>Example

Func _Exit()
    ;cleanup GDI+ resources
    _GDIPlus_GraphicsDispose($g_hGfxCtxt)
    _GDIPlus_GraphicsDispose($g_hGraphics)
    _GDIPlus_BitmapDispose($g_hBitmap)
    _GDIPlus_BitmapDispose($g_hBMP)
    _GDIPlus_Shutdown()
    GUIDelete($g_hGUI)
    Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

Two mistakes:
 
1) $g_hBrush = _GDIPlus_BrushCreateSolid(0xFF0000) -> must be $g_hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
2) you draw the ellipse in wrong order -> must be just after _GDIPlus_GraphicsDrawImage($g_hGfxCtxt, $g_hBMP, $iX, $iY) ;draw bitmap to backbuffer

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>

Global $g_hGUI, $g_hGfxCtxt, $g_hBitmap, $g_hBMP, $g_hGraphics

Example()

Func Example()
    AutoItSetOption("GUIOnEventMode", 1)

    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBGColor format RRGGBB

    $g_hGUI = GUICreate("GDI+ example", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $g_hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    ;create buffered graphics frame set for smoother gfx object movements
    $g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($g_hGUI) ;create a graphics object from a window handle
    $g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $g_hGraphics)
    $g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap)
    Local $iW = 300, $iH = 300
    Local $hHBmp = _ScreenCapture_Capture("", 40, 40, $iW, $iH) ;create a GDI bitmap by capturing an area on desktop
    $g_hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)

    $g_hBMP = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
    Local $iVectorX = Random(1.5, 2.5), $iVectorY = Random(1.5, 2.5) ;define x and y vector
    Local $iX = 0.0, $iY = 0.0 ;define start coordinate

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

    Do
        _GDIPlus_GraphicsClear($g_hGfxCtxt, 0xFF000000 + $iBgColor) ;clear bitmap for repaint
        _GDIPlus_GraphicsDrawImage($g_hGfxCtxt, $g_hBMP, $iX, $iY) ;draw bitmap to backbuffer

        ;this is what is not working #################
        _GDIPlus_GraphicsFillEllipse($g_hGfxCtxt, $iX+50, $iY+50, 30, 30, $g_hBrush)

        _GDIPlus_GraphicsDrawImageRect($g_hGraphics, $g_hBitmap, 0, 0, $iWidth, $iHeight) ;copy drawn bitmap to graphics handle (GUI)
        $iX += $iVectorX ;add x vector to current x position
        $iY += $iVectorY ;add y vector to current y position
        If $iX < 0 Or $iX > ($iWidth - $iW) Then $iVectorX *= -1 ;when x border is reached reverse x vector
        If $iY < 0 Or $iY > ($iHeight - $iH) Then $iVectorY *= -1 ;when y border is reached reverse y vector


    Until Not Sleep(10) ;sleep 10 ms to avoid high cpu usage
EndFunc   ;==>Example

Func _Exit()
    ;cleanup GDI+ resources
    _GDIPlus_GraphicsDispose($g_hGfxCtxt)
    _GDIPlus_GraphicsDispose($g_hGraphics)
    _GDIPlus_BitmapDispose($g_hBitmap)
    _GDIPlus_BitmapDispose($g_hBMP)
    _GDIPlus_Shutdown()
    GUIDelete($g_hGUI)
    Exit
EndFunc   ;==>_Exit

Br,

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

1) Load the jpg

2) create an empty bitmap in the desired dimension -> _GDIPlus_BitmapCreateFromScan0

3) copy from the jpg the portion to the empty bitmap using _GDIPlus_GraphicsDrawImageRectRect

Br,

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 to ask again but I've tried to implement your suggestion in the simplest way I can but I still to be missing something... Can you advise...

thanks

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <Array.au3>




#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 613, 431, 192, 132)
$Graphic1 = GUICtrlCreateGraphic(24, 32, 569, 385)
GUICtrlSetBkColor(-1, 0x000080)
;$Label1 = GUICtrlCreateLabel("Label1", 40, 8, 189, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###





BITMAP()



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd



Func BITMAP()

    Local $filename = "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
    ;Local $filename = _ScreenCapture_Capture("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg", 0, 0, 10, 20)
If $filename = "" Then Exit

;~ 1) Load the jpg
;~ 2) create an empty bitmap in the desired dimension ->
;~ 3) copy from the jpg the portion to the empty bitmap using


$begin = _GDIPlus_BitmapCreateFromScan0(100,100)



_GDIPlus_GraphicsDrawImageRectRect($Graphic1, $begin, 0, 0, 250, 250, 0, 0, 250, 250)



    EndFunc
Link to comment
Share on other sites

Try this:

 

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <Array.au3>



_GDIPlus_Startup()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 613, 431, 192, 132)
GUICtrlSetBkColor(-1, 0x000080)
$iPic = GUICtrlCreatePic("", 24, 32, 569, 385)
$hPic = GUICtrlGetHandle($iPic)
;$Label1 = GUICtrlCreateLabel("Label1", 40, 8, 189, 20)
GUISetState(@SW_SHOW)
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hPic)
#EndRegion ### END Koda GUI section ###

$hBmp = BITMAP()
_GDIPlus_GraphicsDrawImage($hGraphics, $hBmp, 0, 0)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_GraphicsDispose($hGraphics)
            _GDIPlus_BitmapDispose($hBmp)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd



Func BITMAP($filename = "c:\Program Files (x86)\AutoIt3\Examples\GUI\msoobe.jpg", $iW = 569, $iH = 385, $iX = 240, $iY = 200)

;~     $filename = "c:\Program Files (x86)\AutoIt3\Examples\GUI\msoobe.jpg"
    ;Local $filename = _ScreenCapture_Capture("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg", 0, 0, 10, 20)
    If $filename = "" Then Return SetError(1, 0, 0)
    If Not FileExists($filename) Then Return SetError(2, 0, 0)

;~ 1) Load the jpg
    Local $hImage = _GDIPlus_ImageLoadFromFile($filename)

;~ 2) create an empty bitmap in the desired dimension -> _GDIPlus_BitmapCreateFromScan0
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    
;~ 3) copy from the jpg the portion to the empty bitmap using _GDIPlus_GraphicsDrawImageRectRect
    _GDIPlus_GraphicsDrawImageRectRect($hContext, $hImage, $iX, $iY, $iW, $iH, 0, 0, $iW, $iH)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_ImageDispose($hImage)
    Return $hBitmap
EndFunc

Br,

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

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