Jump to content

GDIPlus - Combine images into one


Iczer
 Share

Recommended Posts

I wanted tpo use GDIPlus magic to combine seweral images into one long and then display rezult in PIC-control with scrollbar enabled. Without writing it to disk.

but... some help is welcomed...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 778, 889, 1159, 344)
Global $Pic1 = GUICtrlCreatePic("", 24, 16, 732, 820, BitOR($GUI_SS_DEFAULT_PIC,$SS_SUNKEN,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE))
Global $ButtonLoad = GUICtrlCreateButton("Load Images", 24, 848, 125, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Global $hPic1 = GUICtrlGetHandle ( $Pic1 )

Global $sFolder = "C:\Temp\!_AMTest\"

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

        Case $ButtonLoad
            LoadAll()
            
    EndSwitch
WEnd


Func LoadAll()
    
    Local $iImageCount = 7, $iWmax = 700
    Local $aImage[$iImageCount+1][3], $iW = 0, $iH = 0, $iHPoint = 0
    
    _GDIPlus_Startup()

    For $i = 1 To $iImageCount
        
        $hBitmap = _GDIPlus_BitmapCreateFromFile($sFolder & "\" & StringFormat("%03i",$i) & ".jpg");$hBitmap
        $iPic_W = _GDIPlus_ImageGetWidth($aImage[$i][0])
        $iPic_H = _GDIPlus_ImageGetHeight($aImage[$i][0])
        
        $aImage[$i][2] = Round($iWmax*$iPic_H/$iPic_W,0)
        $aImage[$i][1] = $iWmax
        $aImage[$i][0] = _GDIPlus_ImageResize($hBitmap, $aImage[$i][1], $aImage[$i][2]) ;resize image
        
        $iW = ($iW > $aImage[$i][1])?($iW):($aImage[$i][1])
        $iH += $aImage[$i][2]
        
        _GDIPlus_BitmapDispose($hBitmap)
    Next
    
    $g_hGfx = _GDIPlus_GraphicsCreateFromHWND($hPic1) ;create a graphics object from a window handle
    $g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $g_hGfx) ;create a Bitmap object based on a graphics object
    $g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap) ;get the graphics context of the image / bitmap to draw on image / bitmap
   ; _GDIPlus_GraphicsSetSmoothingMode($g_hGfxCtxt, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)

    For $i = 1 To $iImageCount
        _GDIPlus_GraphicsDrawImageRect($g_hGfxCtxt, $aImage[$i][0], 0, $iHPoint, $aImage[$i][1], $aImage[$i][2])
        $iHPoint += $aImage[$i][2]
    Next
    
    _GDIPlus_GraphicsDrawImage($g_hGfx, $g_hBitmap, 0, 0)

    ; Clean up resources
    For $i = 1 To $iImageCount
        _GDIPlus_BitmapDispose($aImage[$i][0])
    Next
    
    ; Shut down GDI+ library
    _GDIPlus_Shutdown()
EndFunc

 

Link to comment
Share on other sites

What about following

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include "Includes\GUIScrollbars_Size.au3"

Global $iNumOfImages=3
Global $hGDIPBitmaps[$iNumOfImages], $hGFXHandles[$iNumOfImages]
Global $iTotalWidth=0, $iTotalHeight=0;needed to set size of combined image
;create (load) images
_GDIPlus_Startup()
For $i=1 To $iNumOfImages
    $iCurrentColor=Random(0XFF000000,0XFFFFFFFF,1);background color - used to identify images
    $iCurrentWidth=Random(300,500,1)
    $iCurrentHeight=Random(300,500,1)
    ConsoleWrite("width=" & $iCurrentWidth & @CRLF)
    ConsoleWrite("height=" & $iCurrentHeight & @CRLF)
    $hGDIPBitmaps[$i-1]=_GDIPlus_BitmapCreateFromScan0($iCurrentWidth,$iCurrentHeight)
    $hGFXHandles[$i-1]=_GDIPlus_ImageGetGraphicsContext($hGDIPBitmaps[$i-1])
    _GDIPlus_GraphicsClear($hGFXHandles[$i-1],$iCurrentColor)
    $iTotalHeight=$iTotalHeight+$iCurrentHeight;we are combining images vertically
    If $iCurrentWidth>$iTotalWidth Then $iTotalWidth=$iCurrentWidth
Next

$hGDIP_CombinedImage=_GDIPlus_BitmapCreateFromScan0($iTotalWidth,$iTotalHeight)
$hCombined_Graphics=_GDIPlus_ImageGetGraphicsContext($hGDIP_CombinedImage)
_GDIPlus_GraphicsDrawImage($hCombined_Graphics,$hGDIPBitmaps[0],0,0)
$yCoord=_GDIPlus_ImageGetHeight($hGDIPBitmaps[0]);this is new y coord for adding images
_GDIPlus_GraphicsDispose($hGFXHandles[0])
_GDIPlus_BitmapDispose($hGDIPBitmaps[0])
For $i=2 To $iNumOfImages
    ConsoleWrite("i=" & $i & @CRLF)
    ConsoleWrite("yCoord=" & $yCoord & @CRLF)
    _GDIPlus_GraphicsDrawImage($hCombined_Graphics,$hGDIPBitmaps[$i-1],0,$yCoord);add images vertically - only y coordinate is being changed
    $yCoord=$yCoord+_GDIPlus_ImageGetHeight($hGDIPBitmaps[$i-1])
    _GDIPlus_GraphicsDispose($hGFXHandles[$i-1])
    _GDIPlus_BitmapDispose($hGDIPBitmaps[$i-1])
Next

#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 778, 889)
$hChildGUI=GUICreate("Picture holder",500,620,24,16,$WS_POPUP, $WS_EX_MDICHILD, $Form1);this is to make working with scrollbars easier
Global $Pic1 = GUICtrlCreatePic("", 0, 0, 500, 600); BitOR($GUI_SS_DEFAULT_PIC,$SS_SUNKEN,$WS_VSCROLL,$WS_BORDER), BitOR($WS_EX_CLIENTEDGE,$WS_EX_STATICEDGE))
Global $hPic1 = GUICtrlGetHandle ( $Pic1 )

;add scrollbar
_GUIScrollBars_ShowScrollBar($hPic1, $SB_BOTH, False)
_GUIScrollBars_Init($hChildGUI)
$aRet = _GUIScrollbars_Size(0, $iTotalHeight, $hChildGUI)
_GUIScrollBars_SetScrollInfoPage($hChildGUI, $SB_VERT, $aRet[2])
_GUIScrollBars_SetScrollInfoMax($hChildGUI, $SB_VERT, $aRet[3])
_GUIScrollBars_ShowScrollBar($hChildGUI, $SB_HORZ, False)

GUISwitch($Form1)
Global $ButtonLoad = GUICtrlCreateButton("Load Images", 24, 848, 125, 25)
GUISetState(@SW_SHOW)
GUISetState(@SW_SHOW, $hChildGUI)
#EndRegion ### END Koda GUI section ###

_GDIPBitmapToPicControl($hGDIP_CombinedImage,$Pic1)
_GDIPlus_GraphicsDispose($hCombined_Graphics)
_GDIPlus_BitmapDispose($hGDIP_CombinedImage)




GUIRegisterMsg($WM_VSCROLL, "_Scrollbars_WM_VSCROLL")
GUIRegisterMsg($WM_HSCROLL, "_Scrollbars_WM_HSCROLL")
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            Exit

        Case $ButtonLoad

    EndSwitch
WEnd

Func _GDIPBitmapToPicControl($hGDIPBitmap, $idCtrl)

    $hGDIBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hGDIPBitmap)
    Local $tSIZE = _WinAPI_GetBitmapDimension($hGDIBitmap)
    ConsoleWrite(DllStructGetData($tSIZE, 'X') & ' x ' & DllStructGetData($tSIZE, 'Y') & @CRLF)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idCtrl, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDIBitmap))
    _WinAPI_DeleteObject($hGDIBitmap)

EndFunc

Func _Scrollbars_WM_VSCROLL($hWnd, $Msg, $wParam, $lParam)
;~  _LogWrite("Scrolbar")
    #forceref $Msg, $wParam, $lParam
    Local $nScrollCode = BitAND($wParam, 0x0000FFFF)
    Local $iIndex = -1, $yChar, $yPos
    Local $Min, $Max, $Page, $Pos, $TrackPos

    For $x = 0 To UBound($__g_aSB_WindowInfo) - 1
        If $__g_aSB_WindowInfo[$x][0] = $hWnd Then
            $iIndex = $x
            $yChar = $__g_aSB_WindowInfo[$iIndex][3]
            ExitLoop
        EndIf
    Next
    If $iIndex = -1 Then Return 0

    Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT)
    $Min = DllStructGetData($tSCROLLINFO, "nMin")
    $Max = DllStructGetData($tSCROLLINFO, "nMax")
    $Page = DllStructGetData($tSCROLLINFO, "nPage")
    $yPos = DllStructGetData($tSCROLLINFO, "nPos")
    $Pos = $yPos
    $TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")

    Switch $nScrollCode
        Case $SB_TOP
            DllStructSetData($tSCROLLINFO, "nPos", $Min)
        Case $SB_BOTTOM
            DllStructSetData($tSCROLLINFO, "nPos", $Max)
        Case $SB_LINEUP
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - 1)
        Case $SB_LINEDOWN
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + 1)
        Case $SB_PAGEUP
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
        Case $SB_PAGEDOWN
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
        Case $SB_THUMBTRACK
            DllStructSetData($tSCROLLINFO, "nPos", $TrackPos)
    EndSwitch

    DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)
    _GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
    _GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)

    $Pos = DllStructGetData($tSCROLLINFO, "nPos")
    If ($Pos <> $yPos) Then
        _GUIScrollBars_ScrollWindow($hWnd, 0, $yChar * ($yPos - $Pos))
        $yPos = $Pos
    EndIf

    Return $GUI_RUNDEFMSG

EndFunc   ;==>_Scrollbars_WM_VSCROLL

Func _Scrollbars_WM_HSCROLL($hWnd, $Msg, $wParam, $lParam)

    #forceref $Msg, $lParam
    Local $nScrollCode = BitAND($wParam, 0x0000FFFF)
    Local $iIndex = -1, $xChar, $xPos
    Local $Page, $Pos, $TrackPos

    For $x = 0 To UBound($__g_aSB_WindowInfo) - 1
        If $__g_aSB_WindowInfo[$x][0] = $hWnd Then
            $iIndex = $x
            $xChar = $__g_aSB_WindowInfo[$iIndex][2]
            ExitLoop
        EndIf
    Next
    If $iIndex = -1 Then Return 0

    Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_HORZ)
    $Page = DllStructGetData($tSCROLLINFO, "nPage")
    $xPos = DllStructGetData($tSCROLLINFO, "nPos")
    $Pos = $xPos
    $TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")
    Switch $nScrollCode
        Case $SB_LINELEFT
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - 1)
        Case $SB_LINERIGHT
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + 1)
        Case $SB_PAGELEFT
            DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)
        Case $SB_PAGERIGHT
            DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)
        Case $SB_THUMBTRACK
            DllStructSetData($tSCROLLINFO, "nPos", $TrackPos)
    EndSwitch

    DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)
    _GUIScrollBars_SetScrollInfo($hWnd, $SB_HORZ, $tSCROLLINFO)
    _GUIScrollBars_GetScrollInfo($hWnd, $SB_HORZ, $tSCROLLINFO)

    $Pos = DllStructGetData($tSCROLLINFO, "nPos")
    If ($Pos <> $xPos) Then _GUIScrollBars_ScrollWindow($hWnd, $xChar * ($xPos - $Pos), 0)

    Return $GUI_RUNDEFMSG

EndFunc   ;==>_Scrollbars_WM_HSCROLL

I found function _GDIPBitmapToPicControl on the forum under different name - I can not remember original now. For scrollbars I used Scrollbars Made Easy - New version 27 Jan 22.

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