Sign in to follow this  
Followers 0
taotao878

set png image to pic

5 posts in this topic

#1 ·  Posted (edited)

#include <GuiConstantsEX.au3>
#include <GDIPlus.au3>
#include "resources.au3"

_GDIPlus_Startup()

$hGui = GUICreate("show png", 350, 300)
$hPic = GUICtrlCreatePic("", 25, 25, 300, 200)
$Button = GUICtrlCreateButton("view", 130, 250, 75, 21)
GUISetState()


While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Button
            $OpenFile = FileOpenDialog("Select image file", "", "image(*.png)")
            _GUICtrlStatic_SetPicture($OpenFile, $hPic)
    EndSwitch   
WEnd

FUnc _GUICtrlStatic_SetPicture($File, $CtrlId)
       $hImage = _GDIPlus_ImageLoadFromFile($File)
       $hScrDC  = _WinAPI_GetDC(0)
       $hMemDC  = _WinAPI_CreateCompatibleDC($hScrDC)
       $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
       GUICtrlSetImage($CtrlId, "")
      _SetBitmapToCtrl($CtrlId, $hBitmap)
EndFUnc

Edited by taotao878

Share this post


Link to post
Share on other sites



Are youy going to share #include "resources.au3" :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This has nothing with resources.

It just uses internal helper function _SetBitmapToCtrl() from my UDF:

Func _SetBitmapToCtrl($CtrlId, $hBitmap)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local Const $SS_BITMAP = 0xE
    Local Const $GWL_STYLE = -16

    Local $hWnd = GUICtrlGetHandle($CtrlId)
    If $hWnd = 0 Then Return SetError(1, 0, 0)

        ; set SS_BITMAP style to control
    Local $oldStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE)
    If @error Then Return SetError(2, 0, 0)
    DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "long", BitOR($oldStyle[0], $SS_BITMAP))
    If @error Then Return SetError(3, 0, 0)

        Local $oldBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap)
    If @error Then Return SetError(4, 0, 0)
    If $oldBmp[0] <> 0 Then _WinAPI_DeleteObject($oldBmp[0])
    Return 1
EndFunc

So generally you needn't include whole resources.au3 just copy this one function into your script :-)

Edited by Zedna

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Also minor bug:

GUICtrlSetImage($CtrlId, "")

correction:

FUnc _GUICtrlStatic_SetPicture($File, $CtrlId)
$hImage = _GDIPlus_ImageLoadFromFile($File)
  $hScrDC  = _WinAPI_GetDC(0)
  $hMemDC  = _WinAPI_CreateCompatibleDC($hScrDC)
  $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
  GUICtrlSetImage($CtrlId, "")
  _SetBitmapToCtrl($CtrlId, $hBitmap)
EndFUnc
Edited by Zedna

Share this post


Link to post
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
Sign in to follow this  
Followers 0