Jump to content

Display png images


Recommended Posts

Hi everybody :)
I would like to thank UEZ  for the great help he brought us concerning what follows :
When I right click a row in the following ListView, the corresponding image should be displayed in a Splash Window and disappear as soon as the mouse moves. But as you can see below, nothing happens when the image is .png type (as stated in the help file, topics SplashImageOn and GUICtrlCreatePic)

783726549_pngnotvisiblewithSplashImage.png.d852e40e07fdd4de20635b762e749202.png

Now see how it works fine with jpg, gif or bmp :

1909198249_jpgvisiblewithSplashImage.jpg.63d860b4340509401ab1c924b3d658bf.jpg

Gladly UEZ indicated not 1, but 2 different ways to solve this, allowing us to display png files using GDI+
 Let's start with way #1, simulating a Splash Window :

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

$sFileName = @ScriptDir & "\Torus.png"
If Not FileExists($sFileName) Then _
    Exit MsgBox($MB_TOPMOST, "Error", $sFileName & " not found")

_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($sFileName)
$iX = _GDIPlus_ImageGetWidth($hImage)
$iY = _GDIPlus_ImageGetHeight($hImage)

$hGUI = GUICreate("", $iX, $iY, -1, -1, _
    $WS_BORDER + $WS_POPUP, $WS_EX_TOPMOST)
GUISetState()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage, 0, 0, $iX, $iY)

$aPos_Old = MouseGetPos()
Do
    $aPos_New = MouseGetPos()
    Sleep(100)
Until $aPos_Old[0] <> $aPos_New[0] Or $aPos_Old[1] <> $aPos_New[1]

GUIDelete($hGUI)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Now the 2nd way, displaying the png image in a GUI picture control :

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

$sFileName = @ScriptDir & "\Torus.png"
If Not FileExists($sFileName) Then _
    Exit MsgBox($MB_TOPMOST, "Error", $sFileName & " not found")

_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($sFileName)
$iX = _GDIPlus_ImageGetWidth($hImage)
$iY = _GDIPlus_ImageGetHeight($hImage)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)

$hGUI = GUICreate("Display PNG Image in picture control", $iX, $iY, -1, -1, _
    -1, $WS_EX_TOPMOST)
$idPic = GUICtrlCreatePic("", 0, 0, $iX, $iY)
GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap) ; STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            _WinAPI_DeleteObject($hBitmap)
            _GDIPlus_ImageDispose($hImage)
            _GDIPlus_Shutdown()
            Exit

        Case $idPic
            MsgBox($MB_TOPMOST, "Information", "PNG image was clicked")
    EndSwitch
WEnd

The "Torus.png" image can be downloaded just below in case you want to try the scripts. Just rename it "Torus.png" and place it in the same folder than the scripts.

@UEZ : :thumbsup:  and let's hope this will be useful for those who encountered the same issue.

@Mods : I didn't know where to post this comment. If you think it should be placed in another part of the Forum, please be kind enough to move it there, thanks.

Torus.png

 

Edited by pixelsearch
Link to comment
Share on other sites

  • 2 years later...
  • 3 weeks later...

1st things 1st, @UEZ, Thank you for sharing your knowledge.

To follow up on @pixelsearch information, I present a simple GUI that uses #AutoIt3Wrapper_Res_HiDpi=Y.

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=Y
#AutoIt3Wrapper_Res_HiDpi=Y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GDIPlus.au3>

_GDIPlus_Startup()
Global $iScale = DllCall($__g_hGDIPDll, "int", "GdipGetDpiX", "handle", _GDIPlus_GraphicsCreateFromHWND(0), "float*", 0)[2] / 96
; The above line retrieves the current Windows Scaling Factor - If Windows is set to 150% scaling it will return 1.5

GUICreate('Test', 500 * $iScale, 300 * $iScale, 200 * $iScale, 400 * $iScale) ;Create a GUI
GUICtrlCreateButton("DPI testing", 350 * $iScale, 50 * $iScale, 120 * $iScale, 20 * $iScale) ; Create s button

Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Torus.png") ;The PNG image that you want to display
Global $B_Scaled = _GDIPlus_ImageScale($hImage, $iScale, $iScale)
;Torus.png is distributed with AutoIt and is found here - C:\Program Files (x86)\AutoIt3\Examples\GUI

Global $idPic = GUICtrlCreatePic("", 150 * $iScale, 75 * $iScale, _GDIPlus_ImageGetWidth($B_Scaled), _GDIPlus_ImageGetHeight($B_Scaled))
;The above line 1st sets the left/Top coordinates to place the png and then gets scaled image dimensioms for W/H

GUICtrlSendMsg($idPic, 0x0172, 0, _GDIPlus_BitmapCreateHBITMAPFromBitmap($B_Scaled))
; Send the image to the Pic Control

_WinAPI_DeleteObject($hImage) ;some clean up
_GDIPlus_ImageDispose($B_Scaled)
_GDIPlus_Shutdown()

GUISetState(@SW_SHOW, 0)

While 1
    Sleep(50)
    Switch GUIGetMsg()
        Case -3
        Exit
    EndSwitch
WEnd

Using the above scaling for your GUI will present the same GUI no matter if the Windows scaling is set to 100% or 350%
The absolute real bonus of using HiDpi=Y is the quality of the text within your GUI when it is scaled properly.
The text scaling is provided by Windows when the user selects to use HiDpi through the AutoIt3Wrapper.

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