Page 1 of 1

Trouble resizing image according to window size

Posted: Sun Apr 20, 2025 5:11 pm
by Distorted Pixel
Hi,

I have searched the forum and can't find anything to solve my issue.

I have the following program reading an INI file for window settings and setting the window size. Then it sets the window flags according to the window size. That worked great. But, now I can't get the image to resizing to work. My full screen setting is 1920x1080, but I have the INI file set to the windowed setting size 1280x720 to try and get the image to resize to that size. The size of the image is 1920x1080

It gives the following error:

The specified #Image is not initialized

Code: Select all

; main
UsePNGImageDecoder()

Enumeration Windows
  #Window_Main
EndEnumeration

Enumeration Gadgets
  #Gadget_Splash
EndEnumeration

Enumeration Images
  #Img_Splash
EndEnumeration

Global.i WindowWidth, WindowHeight, Flags

; read ini file to set window settings
OpenPreferences("EFS_Preferences.ini")
PreferenceGroup("Window")

WindowWidth = ReadPreferenceLong("Window_Width", 0)
WindowHeight = ReadPreferenceLong("Window_Height", 0)
ClosePreferences()

; check window size for ini file and set correct window flags
If WindowWidth = 1920
  Flags = #PB_Window_BorderLess
Else
  Flags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
EndIf


OpenWindow(#Window_Main, 0, 0, WindowWidth, WindowHeight, "European Football Simulator", Flags)
Img = LoadImage(#Img_Splash, "Images\SplashScreen.png")

If WindowWidth = 1280
Img_Resized = ResizeImage(Img, WindowWidth, WindowHeight)
EndIf

ImageGadget(#Gadget_Splash, 0, 0, WindowWidth, WindowHeight, Img_Resized)

Define QuitMain=0
Repeat
  EventID  =WaitWindowEvent()
  MenuID   =EventMenu()
  GadgetID =EventGadget()
  WindowID =EventWindow()
  
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case #Window_Main
            QuitMain = 1
        EndSelect
    EndSelect
    
  
Until QuitMain = 1
  
Here is what is in the INI file:

Code: Select all

[Global]
ApplicationName = European Football Simulator
Version = 1.0a

[Window]
Window_Width = 1280
Window_Height = 720

Re: Trouble resizing image according to window size

Posted: Sun Apr 20, 2025 5:36 pm
by mk-soft
Not Tested ...

Code: Select all

; main
UsePNGImageDecoder()

Enumeration Windows
  #Window_Main
EndEnumeration

Enumeration Gadgets
  #Gadget_Splash
EndEnumeration

Enumeration Images
  #Img_Splash
EndEnumeration

Global.i WindowWidth, WindowHeight, Flags

; read ini file to set window settings
OpenPreferences("EFS_Preferences.ini")
PreferenceGroup("Window")

WindowWidth = ReadPreferenceLong("Window_Width", 0)
WindowHeight = ReadPreferenceLong("Window_Height", 0)
ClosePreferences()

; check window size for ini file and set correct window flags
If WindowWidth = 1920
  Flags = #PB_Window_BorderLess
Else
  Flags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
EndIf


OpenWindow(#Window_Main, 0, 0, WindowWidth, WindowHeight, "European Football Simulator", Flags)
Img = LoadImage(#Img_Splash, "Images\SplashScreen.png")
If Not Img
  MessageRequester("Error", "Image not found", #PB_MessageRequester_Error)
  End
EndIf

If WindowWidth = 1280
  If Not ResizeImage(#Img_Splash, WindowWidth, WindowHeight)
    MessageRequester("Error", "Image Resize Faild")
  EndIf
EndIf

ImageGadget(#Gadget_Splash, 0, 0, WindowWidth, WindowHeight, ImageID(#Img_Splash))

Define QuitMain=0
Repeat
  EventID  =WaitWindowEvent()
  MenuID   =EventMenu()
  GadgetID =EventGadget()
  WindowID =EventWindow()
  
    Select EventID
      Case #PB_Event_CloseWindow
        Select WindowID
          Case #Window_Main
            QuitMain = 1
        EndSelect
    EndSelect
    
  
Until QuitMain = 1

Re: Trouble resizing image according to window size

Posted: Sun Apr 20, 2025 7:34 pm
by Little John
Either the line

Code: Select all

Img = LoadImage(#Img_Splash, "Images\SplashScreen.png")
must be changed to

Code: Select all

Img = LoadImage(#PB_Any, "Images\SplashScreen.png")
 
 
or the line

Code: Select all

Img_Resized = ResizeImage(Img, WindowWidth, WindowHeight)
must be changed to

Code: Select all

Img_Resized = ResizeImage(#Img_Splash, WindowWidth, WindowHeight)

Re: Trouble resizing image according to window size

Posted: Sun Apr 20, 2025 11:15 pm
by Distorted Pixel
Thank you mk-soft, your example works great

Thank you Little John, for your suggestions. I have chosen to go with Little John's suggestion of changing the ResizeImage statement to the following:

Code: Select all

Img_Resized = ResizeImage(#Img_Splash, WindowWidth, WindowHeight)

Re: Trouble resizing image according to window size

Posted: Mon Apr 21, 2025 11:51 pm
by Distorted Pixel
I have not changed a single thing in the final code from yesterday that actually worked. Why is it not working now? Did PureBasic screw something up in saving it yesterday or loading it today?

Now the image isn't not showing up, just a white screen. It says Image is not initialized again. I even tried deleting the exe file to compile a new one.

Code: Select all

; main
XIncludeFile "GameType.pb"

UsePNGImageDecoder()

Enumeration Windows
  #Window_Main
EndEnumeration

Enumeration Gadgets
  #Gadget_Splash
EndEnumeration

Enumeration Images
  #Img_Splash
EndEnumeration

Global.i WindowWidth, WindowHeight, Flags, Img, Img_Resized

; read ini file to set window settings
OpenPreferences("EFS_Preferences.ini")
PreferenceGroup("Window")

WindowWidth = ReadPreferenceLong("Window_Width", 0)
WindowHeight = ReadPreferenceLong("Window_Height", 0)
ClosePreferences()

; check window size for ini file and set correct window flags
If WindowWidth = 1920
  Flags = #PB_Window_BorderLess
Else
  Flags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
EndIf


OpenWindow(#Window_Main, 0, 0, WindowWidth, WindowHeight, "European Football Simulator", Flags)
Img = LoadImage(#Img_Splash, "Images\SplashScreen.png")

If WindowWidth = 1280
  Img_Resized = ResizeImage(#Img_Splash, WindowWidth, WindowHeight)
EndIf

ImageGadget(#Gadget_Splash, 0, 0, WindowWidth, WindowHeight, Img_Resized)
DisableGadget(#Gadget_Splash, #True)

Define QuitMain=0
Repeat
  EventID  =WaitWindowEvent()
  MenuID   =EventMenu()
  GadgetID =EventGadget()
  WindowID =EventWindow()
  
  Select EventID
    Case #PB_Event_CloseWindow
      Select WindowID
        Case #Window_Main
          QuitMain = 1
      EndSelect
      
    Case #PB_Event_Gadget
      Select GadgetID
        Case #Gadget_Splash
          Select EventType()
            Case #PB_EventType_LeftDoubleClick
            Case #PB_EventType_LeftClick
              GameType()
            Case #PB_EventType_RightDoubleClick
            Case #PB_EventType_RightClick
            Case #PB_EventType_DragStart
            Default
          EndSelect
      EndSelect
  EndSelect
  
Until QuitMain = 1

Re: Trouble resizing image according to window size

Posted: Tue Apr 22, 2025 1:00 am
by RASHAD
Hi
1- Using Preference file to read width and height of any object is not a good idea with DPI
2- Use ExamineDesktops() to get the real width and height of the desktop then a percentage to get your required width and height for your image
3- Check the exciting of the image to get the problem (IsImage())

Good luck

Re: Trouble resizing image according to window size

Posted: Tue Apr 22, 2025 1:46 am
by Distorted Pixel
RASHAD wrote: Tue Apr 22, 2025 1:00 am Hi
1- Using Preference file to read width and height of any object is not a good idea with DPI
2- Use ExamineDesktops() to get the real width and height of the desktop then a percentage to get your required width and height for your image
3- Check the exciting of the image to get the problem (IsImage())

Good luck
Thank you,
I will rework the code tomorrow after work and give it a shot.

I also am having another issue, just to mention it quick.
When I click the left mouse button to go to the next screen, the 1st screen doesn't actually close, it minimizes. I am not using any minimize code.

Re: Trouble resizing image according to window size

Posted: Wed Apr 23, 2025 12:18 am
by Distorted Pixel
What would be the recommended way to save a game's window settings (e.g. full screen or windowed mode) for when the player wants to stop playing and save the game and reload at another time, (.dat) file?

Most people won't want to have to change the window mode every time they load the game.