Jump to content

GDI+ loading 2 png images the proper way


Acce
 Share

Recommended Posts

Hi guys I have tried to figure out how to load .png images the proper way with GDI+ , its my understanding that I need to create a bitmap , canvas but to be honest I dont understand it enough to create my own 

 

Here is the code I have made: 

$base_x = 500
    $base_y = 300
    $start_x = -$iMinScrollx
    $start_y = -$iMinScrolly
    $ix = $start_x - $scroll_x ; pos 0 on img
    $iy = $start_y - $scroll_y ; pos 0 on img
    $xx = ($ix + $base_x) * $fZoom
    $yy = ($iy + $base_y) * $fZoom
    
    ; ,x:-1,y:3
        $base = 10
    $Pos_x = 3 * $base
     ; Load the GDI+ bitmap
 $hImage_1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "x-png-15.png")
 Local $hBitmapTmp = _GDIPlus_BitmapCreateFromFile($hImage_1)
 ; Conver the GDI+ bitmap to a HBitmap
 $aImage = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmapTmp)
 ; Get rid of the GDI+ bitmap
            _GDIPlus_BitmapDispose($hBitmapTmp)
 $x = _GDIPlus_ImageGetWidth($hImage_1)
 $y = _GDIPlus_ImageGetHeight($hImage_1)
     $hImage_resized = _GDIPlus_ImageResize($hImage, $iX, $iY)
 $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
 $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage_1)
 _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage_1, $xx, $yy, 25, 25)

 

Its my understanding that this code draws directly to the GUI and is not the correct way to go about things. 

What I want with this code is to load 2 images one background image and then some picture on top 

But my solution above makes the images flicker evertime they get redrawn . I was hoping someone could show me a working example on how to load 2 GDI images from file ?

 

ANy help at all would be immensely appreciated 

 

I wonder if  

Link to comment
Share on other sites

Thanks for your reply 

Here is the entire code for my current project but its not working like expected :

 

 
    #include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <Misc.au3>
    ; --- Hotkeys ---
HotKeySet("{ESC}", "Terminate")
HotKeySet("^0", "_ResetZoom") ; ctr and 0
    ; --- Vars ---
Global $sx, $z = 5, $sy, $mc2, $f, $scroll_x = 0, $scroll_y, $current_iPos_y, $RQGUI_POS[3] = [280, 325, 0]
Global $g_iIDC = -1, $g_iNewIDC = 0, $bg_c = "404040"
Global $aDim, $hImage = 0, $iMinScrollx, $iMinScrolly
Global $g_aArray = StringSplit("Hand|AppStarting|Arrow|Cross|Help|IBeam|Icon (obsolete)|No|" & _
        "Size (obsolete)|SizeAll|SizeNESW|SizeNS|SizeNWSE|SizeWE|UpArrow|Wait|None", "|", 2) ; The flag parameter is set to flag = 2 as we don't require the total count of the array.
    ; --- MAIN GUI ---
$hGUI = GUICreate('MyGUI', @DesktopWidth - 200, @DesktopHeight - 200, 100, 100, $WS_POPUP)
GUICtrlSetBkColor($hGUI, 0x000000)
    ; GUI width and height
Global $GUI_Pos = WinGetPos($hGUI)
Global $iGUIWidth = $GUI_Pos[2]
Global $iGUIHeight = $GUI_Pos[3]
    GUISetState()
    ; Load GDI+ resources
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "background.jpg")
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hBuffer_Bmp = _GDIPlus_BitmapCreateFromGraphics($iGUIWidth, $iGUIHeight, $hGraphic)
Global Const $hContext = _GDIPlus_ImageGetGraphicsContext($hBuffer_Bmp)
_GDIPlus_GraphicsSetPixelOffsetMode($hContext, 2)
_GDIPlus_GraphicsSetCompositingQuality($hContext, 2)
_GDIPlus_GraphicsSetInterpolationMode($hContext, 5)
    Global $hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixTranslate($hMatrix, $iGUIWidth / 2, $iGUIHeight / 2)
Global Const $hBmp = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    ; Windows messages
GUIRegisterMsg($WM_MOUSEWHEEL, "_Mousewheel")
    ; Catch key presses
Global Const $dll = DllOpen("user32.dll")
    ; --- IMG Dimensions ---
Global $aDim = _GDIPlus_ImageGetDimension($hImage)
Global $iWidth = _GDIPlus_ImageGetWidth($hImage)
Global $iHeight = _GDIPlus_ImageGetHeight($hImage)
Global $fZoom = 1
Global $iMinZoom = 1
Global $iMaxZoom = 20
Global $imgwidth = $iWidth
Global $imgheight = $iHeight
Global $imgxDIFF = 0
Global $imgyDIFF = 0
    Draw2Graphic()
_loadAssets()
Func _loadAssets()
    $base_x = 500
    $base_y = 300
    $start_x = -$iMinScrollx
    $start_y = -$iMinScrolly
    $ix = $start_x - $scroll_x ; pos 0 on img
    $iy = $start_y - $scroll_y ; pos 0 on img
    $xx = ($ix + $base_x) * $fZoom
    $yy = ($iy + $base_y) * $fZoom
    
    ; ,x:-1,y:3
        $base = 10
    $Pos_x = 3 * $base
     ; Load the GDI+ bitmap
 $hImage_1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "x-png-15.png")
 Local $hBitmapTmp = _GDIPlus_BitmapCreateFromFile($hImage_1)
 ; Conver the GDI+ bitmap to a HBitmap
 $aImage = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmapTmp)
 ; Get rid of the GDI+ bitmap
            _GDIPlus_BitmapDispose($hBitmapTmp)
 $x = _GDIPlus_ImageGetWidth($hImage_1)
 $y = _GDIPlus_ImageGetHeight($hImage_1)
     $hImage_resized = _GDIPlus_ImageResize($hImage, $iX, $iY)
 $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
 $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage_1)
 _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage_1, $xx, $yy, 25, 25)
EndFunc
    Do
    $mc2 = MouseGetPos()
    If $g_iIDC = $g_iNewIDC Then
        $g_iIDC = -1
        GUISetCursor($g_iIDC)
    EndIf
    While _IsPressed("01", $dll)
        If $g_iNewIDC <> $g_iIDC Then
            $g_iIDC = $g_iNewIDC
            GUISetCursor($g_iIDC)
        EndIf
        GUICtrlSetCursor(-1, 4)
        $mc3 = MouseGetPos()
        If $mc2[0] <> $mc3[0] Or $mc2[1] <> $mc3[1] Then
            $scroll_x = ($sx + ($mc3[0] - $mc2[0]) / ($z ^ 2 / 16))
            $scroll_y = ($sy + ($mc3[1] - $mc2[1]) / ($z ^ 2 / 16))
                Draw2Graphic()
            $current_iPos_y = $scroll_y
        EndIf
            Sleep(20)
    WEnd
    $sx = $scroll_x
    $sy = $scroll_y
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_GDIPlus_GraphicsDispose($hGraphic)
DllClose($dll)
    Func Draw2Graphic()
     _GDIPlus_GraphicsClear($hContext, "0xFF" & $bg_c)
    $iMinScrollx = (($imgxDIFF / 2)) / $fZoom
    $iMinScrolly = ($imgyDIFF) / $fZoom
        If $scroll_y <= -($iMinScrolly) Then $scroll_y = -($iMinScrolly)
    If $scroll_x <= -(($imgxDIFF / 2)) / $fZoom Then $scroll_x = -(($imgxDIFF / 2)) / $fZoom
    ;ConsoleWrite("$iMinScrolly= " & $iMinScrolly & @LF)
    If $scroll_y >= (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly Then
        $scroll_y = (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly
    EndIf
    If $scroll_x >= (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx Then
        $scroll_x = (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx
    EndIf
    ;ConsoleWrite("Min scrolly=" & - (($imgyDIFF / 2)) / $fZoom & @LF)
        ;ConsoleWrite("GUI Width = " & $GUI_Pos[2] & @LF)
    ;ConsoleWrite("Zoom= " & $fZoom & " img.width:" & $imgwidth & " img.height: " & $imgheight & " img difference =" & $imgxDIFF & @LF)
    ;ConsoleWrite("scroll_y = " & $scroll_y & @LF)
    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, $scroll_x, $scroll_y, $aDim[0], $aDim[1], _
            ($aDim[0] - $aDim[0] * $fZoom) / 2, ($aDim[1] - $aDim[1] * $fZoom), _
            $aDim[0] * $fZoom, $aDim[1] * $fZoom)
_loadAssets()
EndFunc   ;==>Draw2Graphic
    Func _Mousewheel($hWnd, $iMsg, $wParam, $lParam)
    Switch $wParam
        Case 0xFF880000 ;mouse wheel up
            If $fZoom > $iMinZoom Then
                $fZoom -= 0.1
                $imgwidth = $iWidth * $fZoom
                $imgheight = $iHeight * $fZoom
                $imgxDIFF = $imgwidth - $iWidth
                $imgyDIFF = $imgheight - $iHeight
                Draw2Graphic()
            EndIf
        Case 0x00780000
            $fZoom += 0.1
            $imgwidth = $iWidth * $fZoom
            $imgheight = $iHeight * $fZoom
            $imgxDIFF = $imgwidth - $iWidth
            $imgyDIFF = $imgheight - $iHeight
            Draw2Graphic()
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_Mousewheel
func _ResetZoom()
    $fZoom = 1
    $scroll_x = 0
    $scroll_y = 0
    Global $sx, $z = 5, $sy = 0 , $mc2 = 0 , $f = 0, $scroll_x = 0, $scroll_y = 0, $current_iPos_y = 0, $RQGUI_POS[3] = [280, 325, 0]
    $imgwidth = $iWidth * $fZoom
    $imgheight = $iHeight * $fZoom
    $imgxDIFF = $imgwidth - $iWidth
    $imgyDIFF = $imgheight - $iHeight
    Draw2Graphic()
EndFunc
Func Terminate()
    Exit
EndFunc   ;==>Terminate

Link to comment
Share on other sites

You need to use a back buffer like this (I did not modify your code as it was way too much work) :

#include <GUIConstants.au3>
#include <Constants.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Global $size = 400
_GDIPlus_Startup()

;create graphic GUI with back buffer
$hGui = GUICreate("", $size, $size, Default, Default, $WS_POPUP)
GUISetState(@SW_SHOW, $hGui)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($size, $size, $hGraphic)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackBuffer)

; load up first image
$hHBM = _ScreenCapture_Capture ("", 100, 100, $size+100-1, $size+100-1, False)
$hBitmap1 = _GDIPlus_BitmapCreateFromHBITMAP($hHBM)
_WinAPI_DeleteObject ($hHBM)

; load up second image
$hHBM = _ScreenCapture_Capture ("", 400, 400, $size/2+400-1, $size/2+400-1, False)
$hBitmap2 = _GDIPlus_BitmapCreateFromHBITMAP($hHBM)
_WinAPI_DeleteObject ($hHBM)

; draw images into back buffer
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmap1, 0, 0)
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmap2, 200, 200)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $size, $size)

While True
  If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

_GDIPlus_GraphicsDispose($hBackBuffer)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_BitmapDispose($hBitmap1)
_GDIPlus_BitmapDispose($hBitmap2)
_GDIPlus_GraphicsDispose($hGraphic)

_GDIPlus_Shutdown()

 

Link to comment
Share on other sites

thanks for your reply nine , my searching have brought me this direction as well. Thanks for confirming it. I understand modifying my code would be lots of work but I do wonder If you can show your sample when loading the image from file instead of a screenshot as I get really confused by as to where to add in my image since im not doing any screenshots.. 

EDIT: I did some testing now creating a child GUI with transparent background and loading img 2 into that one and that reduced the flickering by 80% i would say, Double buffering technique would be even better ?  

Edited by Acce
Link to comment
Share on other sites

#include <GUIConstants.au3>
#include <Constants.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Global $size = 400
_GDIPlus_Startup()

;create graphic GUI with back buffer
$hGui = GUICreate("", $size, $size, Default, Default, $WS_POPUP)
GUISetState(@SW_SHOW, $hGui)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($size, $size, $hGraphic)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackBuffer)

; load up first image
$hBitmapTmp = _GDIPlus_ImageLoadFromFile("Torus.png")
$hBitmap1 = _GDIPlus_ImageResize($hBitmapTmp, $size, $size)
_GDIPlus_ImageDispose($hBitmapTmp)

; load up second image
$hBitmapTmp = _GDIPlus_ImageLoadFromFile("Torus.png")
$hBitmap2 = _GDIPlus_ImageResize($hBitmapTmp, $size/2, $size/2)
_GDIPlus_ImageDispose($hBitmapTmp)

; draw images into back buffer
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmap1, 0, 0)
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmap2, 200, 200)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $size, $size)

While True
  If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

_GDIPlus_GraphicsDispose($hBackBuffer)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_BitmapDispose($hBitmap1)
_GDIPlus_BitmapDispose($hBitmap2)
_GDIPlus_GraphicsDispose($hGraphic)

_GDIPlus_Shutdown()

That wasn't so hard, was it ?

Link to comment
Share on other sites

I started working on this right away as might see in my original code my images gets drawn everytime the image is scrolled pushed around or on zoom , question is would I need to clear the backbuffer every time I want to redraw the images ?  

Link to comment
Share on other sites

It worked some ordinates are now off , not sure what went wrong compered to my original code but the flickering is now completely gone. Can this be even smoother when using this double buffering technique? I noticed UAZ did something to ge super smooth scrolling which I tried to copy (can be seen in first post) 

 

Here is the current full code although the 2nd image no longer stands still in same spot 

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

; --- Hotkeys ---
HotKeySet("{ESC}", "Terminate")
HotKeySet("^0", "_ResetZoom") ; ctr and 0

; --- Vars ---
Global $sx, $z = 5, $sy, $mc2, $f, $scroll_x = 0, $scroll_y, $current_iPos_y, $RQGUI_POS[3] = [280, 325, 0]
Global $g_iIDC = -1, $g_iNewIDC = 0, $bg_c = "404040"
Global $aDim, $hImage = 0, $iMinScrollx, $iMinScrolly
Global $g_aArray = StringSplit("Hand|AppStarting|Arrow|Cross|Help|IBeam|Icon (obsolete)|No|" & _
        "Size (obsolete)|SizeAll|SizeNESW|SizeNS|SizeNWSE|SizeWE|UpArrow|Wait|None", "|", 2) ; The flag parameter is set to flag = 2 as we don't require the total count of the array.

; --- MAIN GUI ---
$hGUI = GUICreate('MyGUI', @DesktopWidth - 200, @DesktopHeight - 200, 100, 100, $WS_POPUP)
GUICtrlSetBkColor($hGUI, 0x000000)

; GUI width and height
Global $GUI_Pos = WinGetPos($hGUI)
Global $iGUIWidth = $GUI_Pos[2]
Global $iGUIHeight = $GUI_Pos[3]

GUISetState()

; Load GDI+ resources
_GDIPlus_Startup()


;load 1st image
$hBitmapTmp = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "background.jpg")


; --- IMG Dimensions ---
Global $aDim = _GDIPlus_ImageGetDimension($hImage)
Global $iWidth = _GDIPlus_ImageGetWidth($hImage)
Global $iHeight = _GDIPlus_ImageGetHeight($hImage)
Global $fZoom = 1
Global $iMinZoom = 1
Global $iMaxZoom = 20
Global $imgwidth = $iWidth
Global $imgheight = $iHeight
Global $imgxDIFF = 0
Global $imgyDIFF = 0


Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphic)
Global $hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackBuffer)

; draw images into back buffer
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmapTmp, 0, 0)


; Windows messages
GUIRegisterMsg($WM_MOUSEWHEEL, "_Mousewheel")

; Catch key presses
Global Const $dll = DllOpen("user32.dll")



    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hBitmap, $scroll_x, $scroll_y, $aDim[0], $aDim[1], _
            ($aDim[0] - $aDim[0] * $fZoom) / 2, ($aDim[1] - $aDim[1] * $fZoom), _
            $aDim[0] * $fZoom, $aDim[1] * $fZoom)
;Draw2Graphic()
;_loadAssets()
Func _loadAssets()
    $base_x = 500
    $base_y = 300
    $start_x = -$iMinScrollx
    $start_y = -$iMinScrolly
    $ix = $start_x - $scroll_x ; pos 0 on img
    $iy = $start_y - $scroll_y ; pos 0 on img
    $xx = ($ix + $base_x) * $fZoom
    $yy = ($iy + $base_y) * $fZoom


    ; ,x:-1,y:3

    $base = 10
    $Pos_x = 3 * $base

 ; Load the GDI+ bitmap
 $hImage_1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "x-png-15.png")

 $x = _GDIPlus_ImageGetWidth($hImage_1)
 $y = _GDIPlus_ImageGetHeight($hImage_1)

 $hImage_resized = _GDIPlus_ImageResize($hImage_1, 25, 25)
 _GDIPlus_GraphicsDrawImage($hBackBuffer, $hImage_resized, $xx, $yy)


EndFunc

Do
    $mc2 = MouseGetPos()
    If $g_iIDC = $g_iNewIDC Then
        $g_iIDC = -1
        GUISetCursor($g_iIDC)
    EndIf
    While _IsPressed("01", $dll)
        If $g_iNewIDC <> $g_iIDC Then
            $g_iIDC = $g_iNewIDC
            GUISetCursor($g_iIDC)
        EndIf
        GUICtrlSetCursor(-1, 4)
        $mc3 = MouseGetPos()
        If $mc2[0] <> $mc3[0] Or $mc2[1] <> $mc3[1] Then
            $scroll_x = ($sx + ($mc3[0] - $mc2[0]) / ($z ^ 2 / 16))
            $scroll_y = ($sy + ($mc3[1] - $mc2[1]) / ($z ^ 2 / 16))

            Draw2Graphic()
            $current_iPos_y = $scroll_y
        EndIf

        Sleep(20)
    WEnd
    $sx = $scroll_x
    $sy = $scroll_y
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_GDIPlus_GraphicsDispose($hGraphic)
DllClose($dll)

Func Draw2Graphic()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($imgwidth, $imgheight, $hGraphic)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackBuffer)
; draw images into back buffer

_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmapTmp, 0, 0)
_loadAssets()

    $iMinScrollx = (($imgxDIFF / 2)) / $fZoom
    $iMinScrolly = ($imgyDIFF) / $fZoom

    If $scroll_y <= -($iMinScrolly) Then $scroll_y = -($iMinScrolly)
    If $scroll_x <= -(($imgxDIFF / 2)) / $fZoom Then $scroll_x = -(($imgxDIFF / 2)) / $fZoom
    ;ConsoleWrite("$iMinScrolly= " & $iMinScrolly & @LF)
    If $scroll_y >= (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly Then
        $scroll_y = (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly
    EndIf
    If $scroll_x >= (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx Then
        $scroll_x = (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx
    EndIf
    ;ConsoleWrite("Min scrolly=" & - (($imgyDIFF / 2)) / $fZoom & @LF)

    ;ConsoleWrite("GUI Width = " & $GUI_Pos[2] & @LF)
    ;ConsoleWrite("Zoom= " & $fZoom & " img.width:" & $imgwidth & " img.height: " & $imgheight & " img difference =" & $imgxDIFF & @LF)
    ;ConsoleWrite("scroll_y = " & $scroll_y & @LF)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hBitmap, $scroll_x, $scroll_y, $aDim[0], $aDim[1], _
            ($aDim[0] - $aDim[0] * $fZoom) / 2, ($aDim[1] - $aDim[1] * $fZoom), _
            $aDim[0] * $fZoom, $aDim[1] * $fZoom)

EndFunc   ;==>Draw2Graphic

Func _Mousewheel($hWnd, $iMsg, $wParam, $lParam)
    Switch $wParam
        Case 0xFF880000 ;mouse wheel up
            If $fZoom > $iMinZoom Then
                $fZoom -= 0.1
                $imgwidth = $iWidth * $fZoom
                $imgheight = $iHeight * $fZoom
                $imgxDIFF = $imgwidth - $iWidth
                $imgyDIFF = $imgheight - $iHeight
                Draw2Graphic()
            EndIf
        Case 0x00780000
            $fZoom += 0.1
            $imgwidth = $iWidth * $fZoom
            $imgheight = $iHeight * $fZoom
            $imgxDIFF = $imgwidth - $iWidth
            $imgyDIFF = $imgheight - $iHeight
            Draw2Graphic()
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_Mousewheel

func _ResetZoom()
    $fZoom = 1
    $scroll_x = 0
    $scroll_y = 0
    Global $sx, $z = 5, $sy = 0 , $mc2 = 0 , $f = 0, $scroll_x = 0, $scroll_y = 0, $current_iPos_y = 0, $RQGUI_POS[3] = [280, 325, 0]
    $imgwidth = $iWidth * $fZoom
    $imgheight = $iHeight * $fZoom
    $imgxDIFF = $imgwidth - $iWidth
    $imgyDIFF = $imgheight - $iHeight
    Draw2Graphic()
EndFunc
Func Terminate()
    Exit
EndFunc   ;==>Terminate

 

Edited by Acce
Link to comment
Share on other sites

Have looked a more closesly on this now and looks like the issue is with the canvas for some reason it dosnt load the entire images in why is that 

 

Image size is 1920 but when I set the image size like so:

$hBitmap = _GDIPlus_BitmapCreateFromGraphics(1920, $iHeight, $hGraphic)

I do not get the full width ?  any suggestion ? 

Link to comment
Share on other sites

Thanks for swift reply 

not sure how I would make such an snippet cause whole code would be needed to run it , lol 

I manged to fix it  like so:  (I skipped the part with image resize earlier but adding it solved part of the problem)

Func Draw2Graphic()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($imgwidth, $imgheight, $hGraphic)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackBuffer)

; load up first image
$hBitmapTmp = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "background.jpg")
$hBitmap1 = _GDIPlus_ImageResize($hBitmapTmp, $imgwidth, $imgheight)
consolewrite($imgwidth & @lf)
; draw images into back buffer
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmap1, 0, 0)


;_loadAssets()
    $iMinScrollx = ($imgxDIFF) / $fZoom
    $iMinScrolly = ($imgyDIFF) / $fZoom
    ;consolewrite($iGUIWidth & @lf)


    If $scroll_y <= -($iMinScrolly) Then $scroll_y = -($iMinScrolly)
    If $scroll_x <= -(($imgxDIFF / 2)) / $fZoom Then $scroll_x = -(($imgxDIFF / 2)) / $fZoom
    ;ConsoleWrite("$iMinScrolly= " & $iMinScrolly & @LF)
    If $scroll_y >= (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly Then
        $scroll_y = (($imgheight - $iGUIHeight) / $fZoom) - $iMinScrolly
    EndIf
   If $scroll_x >= (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx Then
        $scroll_x = (($imgwidth - $iGUIWidth) / $fZoom) - $iMinScrollx
    EndIf
    ;ConsoleWrite("Min scrolly=" & - (($imgyDIFF / 2)) / $fZoom & @LF)

    ;ConsoleWrite("GUI Width = " & $GUI_Pos[2] & @LF)
    ;ConsoleWrite("Zoom= " & $fZoom & " img.width:" & $imgwidth & " img.height: " & $imgheight & " img difference =" & $imgxDIFF & @LF)
    ConsoleWrite("scroll_x = " & $scroll_x & @LF)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hBitmap, $scroll_x, $scroll_y, $aDim[0], $aDim[1], _
            ($aDim[0] - $aDim[0] * $fZoom) / 2, ($aDim[1] - $aDim[1] * $fZoom), _
            $aDim[0] * $fZoom, $aDim[1] * $fZoom)

EndFunc   ;==>Draw2Graphic

But I also have a function for the zoom level but somehow this dosnt work like indented anymore 

Image width and height get updated here but looks like the canvas not using these new values ? also draw to graphics gets called after so really unsure whats happening here 

Func _Mousewheel($hWnd, $iMsg, $wParam, $lParam)
    Switch $wParam
        Case 0xFF880000 ;mouse wheel up
            If $fZoom > $iMinZoom Then
                $fZoom -= 0.1
                $imgwidth = $iWidth * $fZoom
                $imgheight = $iHeight * $fZoom
                $imgxDIFF = $imgwidth - $iWidth
                $imgyDIFF = $imgheight - $iHeight
                Draw2Graphic()
            EndIf
        Case 0x00780000
            $fZoom += 0.1
            $imgwidth = $iWidth * $fZoom
            $imgheight = $iHeight * $fZoom
            $imgxDIFF = $imgwidth - $iWidth
            $imgyDIFF = $imgheight - $iHeight
            Draw2Graphic()
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_Mousewheel

 

Link to comment
Share on other sites

I think I manged to get it work now. Have tried a few different things and in servel cases I have run into black screen problem I think this is due to that I keep hammering these functions but the 2nd image need cordinates $scroll_x  $scroll_y and zoom level which gets updated whit left click on mouse

Func load_assets with the relative arguments 

$hImage_1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "x-png-15.png")
    $hImage_resized = _GDIPlus_ImageResize($hImage_1, 25, 25)
    
 _GDIPlus_GraphicsClear($hBackBuffer)

Then I would need to load the background like so 

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($imgwidth, $imgheight, $hGraphic)
$hBackBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsClear($hBackBuffer)
    ; load up first image
$hBitmapTmp = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\images\" & "background.jpg")
$hBitmap1 = _GDIPlus_ImageResize($hBitmapTmp, $imgwidth, $imgheight)
_GDIPlus_ImageDispose($hBitmapTmp)
; draw images into back buffer
_GDIPlus_GraphicsDrawImage($hBackBuffer, $hBitmap1, 0, 0)

and finally the 2nd image 

_GDIPlus_GraphicsDrawImage($hBackBuffer, $hImage_resized, $xx, $yy)

 

It seams like running this too much causes black screen Any suggestions ? I thought I could try to update it as little as possible but im not sure that would be possible for the 2nd image  

Edited by Acce
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...