Jump to content

need help with dwawing pic in a window


jvds
 Share

Recommended Posts

i want to use _GDIPlus_BitmapCreateDIBFromBitmap and then draw the image with GUICtrlSendMsg($idPic, $STM_SETIMAGE, 0, $hHBitmap) to $idPic like in this modified example, but if i resize the control it get messed up and picture does not get drawn correcly because the $idPic control size changed, i need a way to fix it because the user can change size constantly at any time even while the bitmap size was set before the picture control changed size like in example, any alternative to   GUICtrlSetResizing($idPic, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM) to fix the image to the picture control?

; PNG work around by UEZ

#include <GDIPlus.au3>

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

Consolewrite (@ProgramFilesDir)
; Create GUI
Local $hMainGUI = GUICreate("Show PNG", 516, 388)

_GUICtrlPic_Create(@ProgramFilesDir&"\AutoIt3\Examples\GUI\msoobe.jpg", 1, 1, 512, 384,8388608)
;~ C:\Program Files (x86)\AutoIt3\Examples\Helpfile\
;~ C:\Program Files (x86)\AutoIt3\Examples\GUI\msoobe.jpg
;~ 512 384

GUISetState(@SW_SHOW)

; Loop until the user exits.
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop

    EndSwitch
WEnd



; #INTERNAL_USE_ONLY#=================================================================================================
; Name...........: _GUICtrlPic_Create
; Description ...: Creates a Picture control for the GUI
; Syntax ........: _GUICtrlPic_Create($sFilename, $iLeft, $iTop, $iWidth, $iHeight, $iStyle = -1 , $iExStyle = -1)
; Parameters ....: $sFilename - Path of image file
; Author ........: UEZ
; Modified.......: Melba23, guinness, jpm
; Remarks .......: PNG image can be used.
; ====================================================================================================================
Func _GUICtrlPic_Create($sFilename, $iLeft, $iTop, $iWidth = -1, $iHeight = -1, $iStyle = -1, $iExStyle = -1)
    _GDIPlus_Startup()
    Local $idPic = GUICtrlCreatePic("", $iLeft, $iTop, $iWidth, $iHeight, $iStyle, $iExStyle)
    Local $hBitmap = _GDIPlus_BitmapCreateFromFile($sFilename)
    If $iWidth = -1 Then $iWidth = _GDIPlus_ImageGetWidth($hBitmap)
    If $iHeight = -1 Then $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
    Local $hBitmap_Resized = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hBMP_Ctxt = _GDIPlus_ImageGetGraphicsContext($hBitmap_Resized)
    _GDIPlus_GraphicsSetInterpolationMode($hBMP_Ctxt, $GDIP_INTERPOLATIONMODE_HIGHQUALITYBICUBIC)
    _GDIPlus_GraphicsDrawImageRect($hBMP_Ctxt, $hBitmap, 0, 0, $iWidth, $iHeight)
    Local $hHBitmap = _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap_Resized)

    GUICtrlSetResizing($idPic, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM)

    WinMove($hMainGUI,'',100,100,600,450)
    ControlMove('','',GUICtrlGetHandle($idPic),1,1,592,420)

    Local $hPrevImage = GUICtrlSendMsg($idPic, $STM_SETIMAGE, 0, $hHBitmap) ; $STM_SETIMAGE = 0x0172
    _WinAPI_DeleteObject($hPrevImage); Delete Prev image if any
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap_Resized)
    _GDIPlus_GraphicsDispose($hBMP_Ctxt)
    _WinAPI_DeleteObject($hHBitmap)
    _GDIPlus_Shutdown()

;~  WinMove($hMainGUI,'',100,100,600,450)
;~  ControlMove('','',GUICtrlGetHandle($idPic),1,1,592,420)

    Return $idPic
EndFunc   ;==>_GUICtrlPic_Create

 

Link to comment
Share on other sites

You mean something like this here?

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

Global Const $STM_SETIMAGE = 0x0172
Global $sPathKey = "HKLM64\SOFTWARE\AutoIt v3\AutoIt\"
If @OSArch = "x64" Then $sPathKey = "HKLM\SOFTWARE\Wow6432Node\AutoIt v3\AutoIt\"
Global $sImage = RegRead($sPathKey, "InstallDir") & "\Examples\GUI\logo4.gif"

_GDIPlus_Startup()
Global $hBmp = _GDIPlus_BitmapCreateFromFile($sImage)
Global $iW = _GDIPlus_ImageGetWidth($hBmp), $iH = _GDIPlus_ImageGetHeight($hBmp)

Global $hGUI = GUICreate("Test", 235, 112, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME), $WS_EX_COMPOSITED)
Global $idPic = GUICtrlCreatePic("", 32, 16, $iW, $iH)
GUICtrlSetResizing(-1, $GUI_DOCKVCENTER + $GUI_DOCKHCENTER)

Global $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)
Global $hB = GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap)
If $hB Then _WinAPI_DeleteObject($hB)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_SIZE, "WM_SIZE")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIRegisterMsg($WM_SIZE, "")
            _WinAPI_DeleteObject($hHBitmap)
            _GDIPlus_BitmapDispose($hBmp)
            _GDIPlus_Shutdown()
            GUIDelete()
            Exit
    EndSwitch
WEnd

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
    #forceref $Msg, $wParam, $lParam
    Local $aSize = ControlGetPos($hWnd, "", $idPic)
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($aSize[2], $aSize[3])
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetInterpolationMode($hContext, 5)
    _GDIPlus_GraphicsDrawImageRect($hContext, $hBmp, 0, 0, $aSize[2], $aSize[3])
    _GDIPlus_GraphicsDispose($hContext)
    Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    Local $hB = GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap)
    If $hB Then _WinAPI_DeleteObject($hB)
    _WinAPI_DeleteObject($hHBitmap)
    _GDIPlus_BitmapDispose($hBitmap)
    Return "GUI_RUNDEFMSG"
EndFunc

 

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

hey, thanks for the example, the image is a stream, here is an example, try resizing with maximize button or dragging the borders, $idPic size changes but every now and then the image get drawn with the previous $idPic height width settings instead of leaving the 5 pixel margins around it, i'm having trouble figuring out how to fix that

#include <Constants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>

Opt("GUIOnEventMode", 1)

Global Const $STM_SETIMAGE = 0x0172

$hGuiWidth = @DesktopWidth/3
$hGuiHeight = @DesktopHeight/3
$idPicWidth = $hGuiWidth - 10
$idPicHeight = $hGuiHeight - 10

Global $hGUI = GUICreate("Test", $hGuiWidth, $hGuiHeight, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME,$WS_MAXIMIZEBOX ), $WS_EX_COMPOSITED)
Global $idPic = GUICtrlCreatePic("", 5, 5, $idPicWidth,$idPicHeight)
GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_SIZE, "WM_SIZE")
GUISetOnEvent($GUI_EVENT_CLOSE, "_GUI_EVENT_CLOSE")

_GDIPlus_Startup()
While 1


    Local $hHBmp = _ScreenCapture_Capture("",0,0,@DesktopWidth,@DesktopHeight,false)
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI bitmap to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp)
    Local $idPicPos = ControlGetPos($hGUI, "", $idPic)
    Local $hImage = _GDIPlus_ImageResize($hBitmap,  $idPicPos[2], $idPicPos[3])
    _GDIPlus_BitmapDispose($hBitmap)
    Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)

    Local $hPrevImage = GUICtrlSendMsg($idPic, $STM_SETIMAGE, 0, $hHBitmap) ; $STM_SETIMAGE = 0x0172
    _WinAPI_DeleteObject($hPrevImage); Delete Prev image if any

    _GDIPlus_BitmapDispose($hBitmap)


WEnd
Func _GUI_EVENT_CLOSE()
    GUIRegisterMsg($WM_SIZE, "")
    GUISetOnEvent($GUI_EVENT_CLOSE, "")
    _GDIPlus_BitmapDispose($hBitmap)
    _WinAPI_DeleteObject($hPrevImage)
    _WinAPI_DeleteObject($hHBmp)

    _GDIPlus_Shutdown()
    GUIDelete()
    Exit
EndFunc

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
    #forceref $Msg, $wParam, $lParam

    ;?????????????????

    Return "GUI_RUNDEFMSG"
EndFunc

 

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