Load image into Gui using GDip Issue

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
magzmedia
Posts: 4
Joined: 14 Aug 2021, 03:48

Load image into Gui using GDip Issue

Post by magzmedia » 26 Aug 2021, 05:14

Hi guys,

I'm one of those people who rarely ask for help as I like to figure things out myself, but I've got totally stumped on what I assume is a really simple problem. All I want to do is to load images into a Gui created in GDIP and all works well until I try to load a new image. Where I've messed up with my controls and variables. here' the script I have:

Code: Select all

#SingleInstance,Force
#NoEnv
ListLines,Off
SetBatchLines,-1
global HB_Button:=[]
pToken:=Gdip_Startup()
hwnd1 := WinExist()

Main := New Custom_Window( x:= "" , y:= "" , w:= 600 , h:= 400 , Name:= "1" , Options:= "+AlwaysOnTop -Caption -DPIScale" , Title:= "Custom Gui Window" , Background_Bitmap:= HB_BITMAP_MAKER() )

Main.Show_Window()



;/*
Gui,1:Add,Text,x6 y3 w190 h24 BackgroundTrans gMove_Window
Gui,1:Add,Text,x360 y5 w15 h15 BackgroundTrans gMin_Window
Gui,1:Add,Text,x380 y5 w15 h15 BackgroundTrans gGuiClose
Gui,1:Color,333333,333333
Gui,1:Font,cWhite s8
;*/
; Add New Buttons
HB_Button.Push( New HB_Flat_Rounded_Button_Type_1( x:=10  , y := 12 , w := 100 , h := 35 , Button_Color := "865ABB" , Button_Background_Color := "333333" , Text := "Load" , Font := "Arial" , Font_Size := 10 " Bold" , Font_Color_Top := "888888" , Font_Color_Bottom := "111111" , Window := "1" , Label := "Load_image" , Default_Button := 1 , Roundness:=2 ) )






;Start the Button Hover Watch Timer
SetTimer, HB_Button_Hover , 50

return
GuiClose:
GuiContextMenu:
*Esc::
	ExitApp

Load_image:
Gui Submit, nohide
FileSelectFile, picture, 3, , Open a file, Image Files (JPG;*.jpg;*.PNG;*.png)
pBitmap := Gdip_CreateBitmapFromFile(picture)
Width := Gdip_GetImageWidth(pBitmap)
Height := Gdip_GetImageHeight(pBitmap)
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
Gui, 1: Add, Picture, x10 y+30 w%Width% h%Height% 0xE vMyPicture
GuiControlGet, hwnd, hwnd, MyPicture
SetImage(hwnd, hBitmap)
Gui, 1: Show, AutoSize
return

HB_BITMAP_MAKER(){
	;Bitmap Created Using: HB Bitmap Maker
	pBitmap := Gdip_CreateBitmap( 600 , 400 ) , G := Gdip_GraphicsFromImage( pBitmap ) , Gdip_SetSmoothingMode( G , 4 )
	Brush := Gdip_BrushCreateSolid( "0xFF333333" ) , Gdip_FillRectangle( G , Brush , 0 , 0 , 600 , 400 ) , Gdip_DeleteBrush( Brush )
	Gdip_DeleteGraphics( G )
	return pBitmap
}

Move_Window(){
	PostMessage,0xA1,2
}

Min_Window(){
	Gui,1:Minimize
}





Class Custom_Window	{
	__New(x:="",y:="",w:=300,h:=200,Name:=1,Options:="+AlwaysOnTop -Caption -DPIScale",Title:="",Background_Bitmap:=""){
		This.X:=x
		This.Y:=y
		This.W:=w
		This.H:=h
		This.Name:=Name
		This.Title:=Title
		This.Options:=Options
		This.Background_Bitmap:=Background_Bitmap
		This.Create_Window()
	}
	Create_Window(){
		Gui,% This.Name ":New",%  This.Options " +LastFound"
		This.Hwnd:=WinExist()
		if(This.Background_Bitmap)
			This.Draw_Background_Bitmap()
	}
	Draw_Background_Bitmap(){
		This.Bitmap:=Gdip_CreateHBITMAPFromBitmap(This.Background_Bitmap)
		Gdip_DisposeImage(This.Background_Bitmap)
		Gui,% This.Name ":Add",Picture,% "x0 y0 w" This.W " h" This.H " 0xE"
		GuiControlGet,hwnd,% This.Name ":hwnd",Static1
		This.Background_Hwnd:=hwnd
		SetImage(This.Background_Hwnd,This.Bitmap)
	}
	Show_Window(){
		if(This.X&&This.Y)
			Gui,% This.Name ":Show",% "x" This.X " y" This.Y " w" This.W " h" This.H,% This.Title
		else if(This.X&&!This.Y)
			Gui,% This.Name ":Show",% "x" This.X  " w" This.W " h" This.H,% This.Title
		else if(!This.X&&This.Y)
			Gui,% This.Name ":Show",% "y" This.Y  " w" This.W " h" This.H,% This.Title
		else
			Gui,% This.Name ":Show",% " w" This.W " h" This.H,% This.Title
	}
}



Test_Button_Press:
	;-----------------------------------------------------
	; In the button label, add the focus grab line
	GuiControl , % HB_Button[ A_GuiControl ].Window ": Focus" , % HB_Button[ A_GuiControl ].Hwnd
	; Add the button press function call and test
	if( ! HB_Button[ A_GuiControl ].Draw_Pressed() )
		return	
	;----------------------------------------------
	
	
	Run,calc.exe
	return









;-----------------------------------------------------------------
;Add the hover fuction to the bottom of your script (or go lib route if you know how to)
HB_Button_Hover(){
	Static Index , Hover_On
	MouseGetPos,,,, ctrl , 2
	if( ! Hover_On && ctrl ){
		loop , % HB_Button.Length()
			if( ctrl = HB_Button[ A_Index ].hwnd )
				HB_Button[ A_Index ].Draw_Hover() , Index := A_Index , Hover_On := 1 , break
	}else if( Hover_On = 1 )
		if( ctrl != HB_Button[ Index ].Hwnd )
			HB_Button[ Index ].Draw_Default() , Hover_On := 0
}
;-----------------------------------------------------------------



;-----------------------------------------------------------------
;Add the button class to the bottom of your script (or go lib route if you know how to)

class HB_Flat_Rounded_Button_Type_1	{
	__New( x := 10 , y := 10 , w := 150 , h := 40 , Button_Color := "FF0000" , Button_Background_Color := "222222" , Text := "Button" , Font := "Arial" , Font_Size := 16 , Font_Color_Top := "000000" , Font_Color_Bottom := "FFFFFF" , Window := "1" , Label := "" , Default_Button := 1, Roundness:=5 ){
		This.Roundness:=Roundness
		This.Text_Color_Top := "0xFF" Font_Color_Top 
		This.Text_Color_Bottom := "0xFF" Font_Color_Bottom 
		This.Font := Font 
		This.Font_Size := Font_Size 
		This.Text := Text
		This.X := x 
		This.Y := y 
		This.W := w 
		This.H := h 
		This.Button_Background_Color := "0xFF" Button_Background_Color 
		This.Button_Color := "0xFF" Button_Color 
		This.Window := Window
		This.Label := Label 
		This.Default_Button := Default_Button 
		This.Create_Default_Bitmap() 
		This.Create_Hover_Bitmap() 
		This.Create_Pressed_Bitmap() 
		This.Create_Trigger()
		sleep, 20
		This.Draw_Default()
	}
	Create_Trigger(){
		global
		num := HB_Button.Length()+1
		Gui , % This.Window ": Add" , Picture , % "x" This.X " y" This.Y " w" This.W " h" This.H " hwndHwnd v" Num " g" This.Label " 0xE"
		This.Number := Num , This.Hwnd := Hwnd
	}
	Create_Default_Bitmap(){
		;Bitmap Created Using: HB Bitmap Maker
		pBitmap:=Gdip_CreateBitmap( This.W , This.H ) ;96x29
		 G := Gdip_GraphicsFromImage( pBitmap )
		Gdip_SetSmoothingMode( G , 2 )
		Brush := Gdip_BrushCreateSolid( This.Button_Background_Color )
		Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF61646A" , "0xFF2E2124" , 1 , 1 )
		Gdip_FillRoundedRectangle( G , Brush , 0 , 1 , This.W , This.H-3 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		
		;---------------------------------------------------
		if(This.Default_Button)
			Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF4C4F54" , "0xFF35373B" , 1 , 1 )
		else 	
			Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , This.Button_Color , "0xFF35373B" , 1 , 1 )
		;-------------------------------------------	
			
		Gdip_FillRoundedRectangle( G , Brush , 1 , 2 , This.W-2 , This.H-5 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		Pen := Gdip_CreatePen( "0xFF1A1C1F" , 1 )
		Gdip_DrawRoundedRectangle( G , Pen , 0 , 0 , This.W-1 , This.H-3 , This.Roundness )
		Gdip_DeletePen( Pen )
		Brush := Gdip_BrushCreateSolid( This.Text_Color_Bottom )
		Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y2 " , This.Font , This.W , This.H-1 )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Text_Color_Top )
		Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y1 " , This.Font , This.W , This.H-1 )
		Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G )
		This.Default_Bitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
		Gdip_DisposeImage(pBitmap)
	}
	Create_Hover_Bitmap(){
		;Bitmap Created Using: HB Bitmap Maker
		pBitmap:=Gdip_CreateBitmap( This.W , This.H ) ;96x29
		 G := Gdip_GraphicsFromImage( pBitmap )
		Gdip_SetSmoothingMode( G , 2 )
		Brush := Gdip_BrushCreateSolid( This.Button_Background_Color )
		Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 )
		Gdip_DeleteBrush( Brush )
		
		Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF61646A" , "0xFF2E2124" , 1 , 1 )
		Gdip_FillRoundedRectangle( G , Brush , 0 , 1 , This.W , This.H-3 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		;---------------------------------------------------------------------------
		if(This.Default_Button)
			Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF55585D" , "0xFF3B3E41" , 1 , 1 )
			
		else 
			Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0x44A826A2" , "0xFF3B3E41" , 1 , 1 )
		;----------------------------------------------------------------	
		Gdip_FillRoundedRectangle( G , Brush , 1 , 2 , This.W-2 , This.H-5 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		Pen := Gdip_CreatePen( "0xFF1A1C1F" , 1 )
		Gdip_DrawRoundedRectangle( G , Pen , 0 , 0 , This.W-1 , This.H-3 , This.Roundness )
		Gdip_DeletePen( Pen )
		Brush := Gdip_BrushCreateSolid( This.Text_Color_Bottom )
		Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y2" , This.Font , This.W , This.H-1 )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Text_Color_Top )
		Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y1" , This.Font , This.W , This.H-1 )
		Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G )
		This.Hover_Bitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
		Gdip_DisposeImage(pBitmap)
	}
	Create_Pressed_Bitmap(){
		pBitmap:=Gdip_CreateBitmap( This.W , This.H ) 
		 G := Gdip_GraphicsFromImage( pBitmap )
		Gdip_SetSmoothingMode( G , 2 )
		Brush := Gdip_BrushCreateSolid( This.Button_Background_Color )
		Gdip_FillRectangle( G , Brush , -1 , -1 , This.W+2 , This.H+2 )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_CreateLineBrushFromRect( 0 , 0 , This.W , This.H , "0xFF2A2C2E" , "0xFF45474E" , 1 , 1 )
		Gdip_FillRoundedRectangle( G , Brush , 0 , 1 , This.W , This.H-3 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( "0xFF2A2C2E" )
		Gdip_FillRoundedRectangle( G , Brush , 0 , 0 , This.W , This.H-8 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( "0xFF46474D" )
		Gdip_FillRoundedRectangle( G , Brush , 0 , 7 , This.W , This.H-8 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		;------------------------------------------------------------------
		if(This.Default_Button)
			Brush := Gdip_CreateLineBrushFromRect( 5 , 3 , This.W ,This.H-7 , "0xFF333639" , "0xFF43474B" , 1 , 1 )
		else 
			Brush := Gdip_CreateLineBrushFromRect( 5 , 3 , This.W ,This.H-7 , "0xFF333639" , "0xFF0066aa" , 1 , 1 )
		;-----------------------------------------------------------------------
		Gdip_FillRoundedRectangle( G , Brush , 1 , 2 , This.W-3 , This.H-6 , This.Roundness )
		Gdip_DeleteBrush( Brush )
		Pen := Gdip_CreatePen( "0xFF1A1C1F" , 1 )
		Gdip_DrawRoundedRectangle( G , Pen , 0 , 0 , This.W-1 , This.H-3 , This.Roundness )
		Gdip_DeletePen( Pen )
		Brush := Gdip_BrushCreateSolid( This.Text_Color_Bottom )
		Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x1 y3" , This.Font , This.W , This.H-1 )
		Gdip_DeleteBrush( Brush )
		Brush := Gdip_BrushCreateSolid( This.Text_Color_Top )
		Gdip_TextToGraphics( G , This.Text , "s" This.Font_Size " Center vCenter c" Brush " x0 y2" , This.Font , This.W , This.H-1 )
		Gdip_DeleteBrush( Brush )
		Gdip_DeleteGraphics( G )
		This.Pressed_Bitmap := Gdip_CreateHBITMAPFromBitmap( pBitmap )
		Gdip_DisposeImage( pBitmap )
	}
	Draw_Default(){
		SetImage( This.Hwnd , This.Default_Bitmap )
	}
	Draw_Hover(){
		SetImage( This.Hwnd , This.Hover_Bitmap )
	}
	Draw_Pressed(){
		SetImage( This.Hwnd , This.Pressed_Bitmap )
		SetTimer , HB_Button_Hover , Off
		While( GetKeyState( "LButton" ) )
			sleep , 10
		SetTimer , HB_Button_Hover , On
		MouseGetPos,,,, ctrl , 2
		if( This.Hwnd != ctrl ){
			This.Draw_Default()
			return False
		}else	{
			This.Draw_Hover()
			return true
		}
	}
}
Any help would be appreciated here, I've just been going around in circles and getting nowhere.

Many thanks Rob

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Load image into Gui using GDip Issue

Post by teadrinker » 26 Aug 2021, 12:51

Hi
Why do you want to load images using GDIp?

magzmedia
Posts: 4
Joined: 14 Aug 2021, 03:48

Re: Load image into Gui using GDip Issue

Post by magzmedia » 26 Aug 2021, 14:44

Oh hey, thanks for coming back to me. I just love the guis that can be created with GDIP, it's a presentation issue I guess. Is it that complicated, what I have asked?

Thanks Rob :)

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Load image into Gui using GDip Issue

Post by teadrinker » 26 Aug 2021, 15:41

magzmedia wrote:

Code: Select all

Gui, 1: Add, Picture, x10 y+30 w%Width% h%Height% 0xE vMyPicture
If you use a standard picture control, there is no sence using GDIp to load an image into it.
You can do like this:

Code: Select all

Gui, 1: Add, Picture, x10 y+30 vMyPicture, % imageFilePath
and next time load another picture using GuiControl.

magzmedia
Posts: 4
Joined: 14 Aug 2021, 03:48

Re: Load image into Gui using GDip Issue

Post by magzmedia » 26 Aug 2021, 17:04

Thank you Teadrinker, much appreciated :)

magzmedia
Posts: 4
Joined: 14 Aug 2021, 03:48

Re: Load image into Gui using GDip Issue

Post by magzmedia » 28 Aug 2021, 05:35

Hey guys, I managed to do it in the end with GDip and very happy with the result. Here's the code should anyone need it at any time. Thanks :)

Code: Select all

#SingleInstance, Force
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
SetBatchLines,-1
global HB_Button:=[]
pToken:=Gdip_Startup()
;Start up gdip
pToken := Gdip_Startup()
;+0xE is necessary for setimage
;If you set an explicit size for the control (ex w100 h100),
;you can use +0x40 to fit the image to the control ;
Gui Add, Picture, x220 y12 w600 h600 vMyPicture,
Gui, Add, Pic, x220 y12 w600 h600 +0x40 hwndhpControl +0xE
Gui, Show, w850 h600
Gui Add, Button, gload x24 y60 w80 h23, Load
;To shutdown gdip before the program closes
OnExit, exit
return
exit:
Gdip_Shutdown(pToken)
Exitapp
return
load:
Gui, Submit, nohide
FileSelectFile, MyPicture, 3, , Open a file, Image Files (JPG;*.jpg;*.PNG;*.png)
;Perhaps you have a different naming scheme, this was the format of my images
pBitmap := Gdip_CreateBitmapFromFile(MyPicture)
;SetImage won't take pBitmap
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
GuiControlGet, hwnd, hwnd, MyPicture
SetImage(hwnd, hBitmap)
;hpControl is the hwnd of the picture control, hBitmap is the image from the disk
;This is what actually puts the image into the picture control.
SetImage(hpControl, hBitmap)
;Free memory
DeleteObject(hBitmap)
Gdip_DisposeImage(pBitmap)
return

Flowgun
Posts: 75
Joined: 25 Aug 2022, 09:42

Re: Load image into Gui using GDip Issue

Post by Flowgun » 24 Mar 2023, 21:08

magzmedia wrote: Hey guys, I managed to do it in the end with GDip and very happy with the result. Here's the code should anyone need it at any time. Thanks :)

Code: Select all

#SingleInstance, Force
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
SetBatchLines,-1
global HB_Button:=[]
pToken:=Gdip_Startup()
;Start up gdip
pToken := Gdip_Startup()
;+0xE is necessary for setimage
;If you set an explicit size for the control (ex w100 h100),
;you can use +0x40 to fit the image to the control ;
Gui Add, Picture, x220 y12 w600 h600 vMyPicture,
Gui, Add, Pic, x220 y12 w600 h600 +0x40 hwndhpControl +0xE
Gui, Show, w850 h600
Gui Add, Button, gload x24 y60 w80 h23, Load
;To shutdown gdip before the program closes
OnExit, exit
return
exit:
Gdip_Shutdown(pToken)
Exitapp
return
load:
Gui, Submit, nohide
FileSelectFile, MyPicture, 3, , Open a file, Image Files (JPG;*.jpg;*.PNG;*.png)
;Perhaps you have a different naming scheme, this was the format of my images
pBitmap := Gdip_CreateBitmapFromFile(MyPicture)
;SetImage won't take pBitmap
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
GuiControlGet, hwnd, hwnd, MyPicture
SetImage(hwnd, hBitmap)
;hpControl is the hwnd of the picture control, hBitmap is the image from the disk
;This is what actually puts the image into the picture control.
SetImage(hpControl, hBitmap)
;Free memory
DeleteObject(hBitmap)
Gdip_DisposeImage(pBitmap)
return
Thank you! apparently the "+0xE" and "+0x40" are needed for this to work!

Post Reply

Return to “Ask for Help (v1)”