Trouble resizing image according to window size

Just starting out? Need help? Post your questions and find answers here.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 312
Joined: Sun Aug 29, 2021 4:34 am

Trouble resizing image according to window size

Post 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
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Trouble resizing image according to window size

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Little John
Addict
Addict
Posts: 4802
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trouble resizing image according to window size

Post 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)
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 312
Joined: Sun Aug 29, 2021 4:34 am

Re: Trouble resizing image according to window size

Post 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)
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 312
Joined: Sun Aug 29, 2021 4:34 am

Re: Trouble resizing image according to window size

Post 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
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Trouble resizing image according to window size

Post 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
Egypt my love
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 312
Joined: Sun Aug 29, 2021 4:34 am

Re: Trouble resizing image according to window size

Post 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.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 312
Joined: Sun Aug 29, 2021 4:34 am

Re: Trouble resizing image according to window size

Post 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.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply