Resizing an image to fullscreen (à la Windows Desktop)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

I'm pretty sure this topic is an old topic... but I'm stuck. Really stuck !
I've searched the forum for relevant material, but I haven't found anything that would help me out of my rut. Probabby because i'm not using adequate search terms.

Any help would be appreciated.

here's the flow of development leading to the problem :

Code: Select all

  ; • open a window
  ; • insert an image gadget
  ; • load an image 
  ; • insert it into the image gadget
  ; • display the image
Up to this point, everything works nicely and smoothly. No sweat at all.
But then things bog down : my loaded image remains stubbornly at its initial size...

Code: Select all

  ; • resize the image so it fills the entire screen  <<<---- How can this be done ? 
I thought this last step would be as simple as what comes before, but obviously it's not.
Any idea pointing me in the right direction would be appreciated...
Last edited by Blue on Thu Mar 30, 2023 3:27 pm, edited 1 time in total.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by RASHAD »

Hi Blue
Long time no see
Seem that you are very lazy or very busy with something else Grand Pa :D

Use Dropdown Menu

Code: Select all

UseJPEG2000ImageDecoder()
UseJPEG2000ImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

ExamineDesktops()

Procedure sizeCB()
  If IsImage(0)
    CopyImage(0,1)
    ResizeImage(1,WindowWidth(0),WindowHeight(0))
    SetGadgetState(0,ImageID(1))
  EndIf
EndProcedure

flags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
OpenWindow(0,0,0,400,300,"Test",Flags)

If CreatePopupImageMenu(0)
  MenuItem(1, "Open",LoadImage(10, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
  MenuItem(2, "Copy",LoadImage(20, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Copy.png"))
  MenuItem(3, "Paste",LoadImage(30, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Paste.png"))
  MenuBar()
  OpenSubMenu("Resize Image")
    MenuItem(4, "Full Screen",LoadImage(40, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
    MenuItem(5, "Normal Screen",LoadImage(50, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
  CloseSubMenu()
  MenuBar()
  MenuItem( 6, "Quit",LoadImage(60, #PB_Compiler_Home + "examples/sources/Data/ToolBar/New.png"))
EndIf

ImageGadget(0,0,0,0,0,0)
DisableGadget(0,1) 
BindEvent(#PB_Event_SizeWindow,@sizeCB())
Repeat
           
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1
      
      Case #PB_Event_Timer
      
      Case #PB_Event_RightClick
        DisplayPopupMenu(0, WindowID(0))
      
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            Filename$ = OpenFileRequester("Image", "", "Image|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tiff", 0)
            If FileSize(Filename$) >= 0
              result = LoadImage(0,Filename$)
              If result
                CopyImage(0,1)
                ResizeImage(1,WindowWidth(0),WindowHeight(0))
                SetGadgetState(0,ImageID(1))
              EndIf
            Else
              MessageRequester("Error","No image loaded",#MB_OK)
            EndIf
          Case 2
          Case 3
          Case 4
            nx = WindowX(0)
            ny = WindowY(0)
            nw = WindowWidth(0)
            nh = WindowHeight(0)
            SetWindowLongPtr_(WindowID(0), #GWL_STYLE, GetWindowLongPtr_(WindowID(0), #GWL_STYLE)&~ #WS_THICKFRAME &~ #WS_DLGFRAME)
            ResizeWindow(0,0,0,DesktopWidth(0),DesktopHeight(0))
          Case 5
            SetWindowLongPtr_(WindowID(0), #GWL_STYLE, GetWindowLongPtr_(WindowID(0), #GWL_STYLE)|#WS_THICKFRAME | #WS_DLGFRAME)
            ResizeWindow(0,nx,ny,nw,nh)
          Case 6
            End                     
        EndSelect
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 1            
          EndSelect          

  EndSelect 

Until Quit = 1
End
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5039
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by idle »

an alternative if you wanted a resizable window with a window screen.

Code: Select all

InitSprite()
InitKeyboard() 
InitMouse() 

UseJPEG2000ImageDecoder()
UseJPEG2000ImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

ExamineDesktops() 

Global gflags,gEvent
Global gwidth = DesktopWidth(0)
Global gheight = DesktopHeight(0) 
Global gImagefile.s = OpenFileRequester("open image",GetUserDirectory(#PB_Directory_Pictures),"*.*",1) 

If gImageFile <> "" 
  
  gflags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
  
  OpenWindow(0,0,0,400,300,"Test",gFlags)
  OpenWindowedScreen(WindowID(0),0,0,gWidth,gHeight)
  ReleaseMouse(1)
  
  If LoadImage(0,gImagefile)  
    If CreateSprite(0,ImageWidth(0),ImageHeight(0)) 
      StartDrawing(SpriteOutput(0))
      DrawImage(ImageID(0),0,0,ImageWidth(0),ImageHeight(0))
      StopDrawing()  
    EndIf    
  EndIf 
  
  Repeat  
    Repeat 
      gEvent = WindowEvent() 
      Select gEvent 
        Case #PB_Event_CloseWindow
          End 
      EndSelect
    Until gEvent = 0
    
    ExamineKeyboard() 
    ExamineMouse() 
    
    If IsSprite(0) 
       ZoomSprite(0,WindowWidth(0),WindowHeight(0))
    EndIf   
    
    DisplaySprite(0,0,0) 
    
    FlipBuffers() 
    
  Until KeyboardPushed(#PB_Key_Escape)  
  
EndIf 


User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

Thank you idle
Thank you RASHAD

I’ll examine closely and work at both your * very complete * (wow !) proposed solutions and get back to you over time. Looking at what you offer here, I wonder why I even bother to program at all. I should just ask you both for a line of code… or 20 !

Note to RASHAD : tsk, tsk…. sleeping 16 hours a day does not make me lazy. It just means that I finally get to live my cat’s life, an old cat’s life, granted, but a cat’s life just the same :D My doctor calls it “living the dream”; unfortunately, I think he means that literally, the old dog…
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

ATTENTION : RASHAD

Studying and testing your code made me discover what I was doing wrong in my own code, very similar to yours.
I was overlooking the CopyImage(0,1) step (line 14 of your code) and just trying to work on the original ioaded image.
Once I introduced that step and adjusted my code, I got the result I wanted.

So, again, many thanks.
Last edited by Blue on Thu Mar 30, 2023 3:28 pm, edited 1 time in total.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

ATTENTION : idle

Your code opened my eyes to something i hadn't paid any attention to, all these years.
Until i played around and figured it out the very complete code you suggested to me, I thought of Sprites as just weird UFOs, things that flew and buzzed around to make games enjoyable. Now, thanks to you, they appear a lot more interesting and practical. So thanks a lot for opening this new box of toys for me. I'm truly having fun again...

Oh, and, yes, your code, with a few tweaks, does exactly what i was looking and hoping for. Great stuff ! great help !
Many thanks.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

I gather, from RASHAD's and idle's suggested solutions above, that neither one of you is currently using a display that requires fiddling with scaling dimensions in order to adapt the output of your code to different DPi resolutions. If you can afford it, you may find it interesting — and challenging — to update your displays to hardware with resolutions higher than the traditional 1:1 ratio. You'll see that it changes the ball game quite a bit and presents technical difficulties that you may have not yet encountered.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by RASHAD »

OK Blue :D
No problem
Enable DPI aware

Code: Select all

Global dpix.d,dpiy.d
dpix = DesktopResolutionX()
dpiy = DesktopResolutionY()

UseJPEG2000ImageDecoder()
UseJPEG2000ImageEncoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

ExamineDesktops()

Procedure sizeCB()
  If IsImage(0)
    CopyImage(0,1)
    ResizeImage(1,WindowWidth(0)*dpix,WindowHeight(0)*dpiy)
    SetGadgetState(0,ImageID(1))
  EndIf
EndProcedure

flags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
OpenWindow(0,0,0,400,300,"Right Mouse for Context Menu",Flags)

If CreatePopupImageMenu(0)
  MenuItem(1, "Open",LoadImage(10, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
  MenuItem(2, "Copy",LoadImage(20, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Copy.png"))
  MenuItem(3, "Paste",LoadImage(30, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Paste.png"))
  MenuBar()
    MenuItem(4, "Save As..",LoadImage(40, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Save.png"))
  MenuBar()
  OpenSubMenu("Resize Image")
    MenuItem(5, "Full Screen",LoadImage(50, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
    MenuItem(6, "Normal Screen",LoadImage(60, #PB_Compiler_Home + "examples/sources/Data/ToolBar/Open.png"))
  CloseSubMenu()
  MenuBar()
  MenuItem( 7, "Quit",LoadImage(70, #PB_Compiler_Home + "examples/sources/Data/ToolBar/New.png"))
EndIf

ImageGadget(0,0,0,0,0,0)
DisableGadget(0,1) 
BindEvent(#PB_Event_SizeWindow,@sizeCB())
Repeat
           
  Select WaitWindowEvent()
      
      Case #PB_Event_CloseWindow
            Quit = 1
      
      Case #PB_Event_Timer
      
      Case #PB_Event_RightClick
        DisplayPopupMenu(0, WindowID(0),DesktopMouseX(),DesktopMouseY())
      
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            Filename$ = OpenFileRequester("Image", "", "Image|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tiff", 0)
            If FileSize(Filename$) >= 0
              result = LoadImage(0,Filename$)
              If result
                CopyImage(0,1)
                ResizeImage(1,WindowWidth(0)*dpix,WindowHeight(0)*dpiy)
                SetGadgetState(0,ImageID(1))
              EndIf
            Else
              MessageRequester("Error","No image loaded",#MB_OK)
            EndIf
          
          Case 2
            If IsImage(1)
              SetClipboardImage(1)
            EndIf
                        
          Case 3
            If GetClipboardImage(0)
              CopyImage(0,1)
              ResizeImage(1,WindowWidth(0)*dpix,WindowHeight(0)*dpiy)
              SetGadgetState(0,ImageID(1))
            EndIf
            
          Case 4
            If IsImage(1)
              imgH = GetGadgetState(0)
              CreateImage(10,WindowWidth(0)*dpix,WindowHeight(0)*dpiy,24)
              StartDrawing(ImageOutput(10))
                DrawImage(imgH,0,0)
              StopDrawing()
              SaveImage(10,GetTemporaryDirectory()+"Test.bmp")
              FreeImage(10)
            EndIf
            
          Case 5
            nx = WindowX(0)
            ny = WindowY(0)
            nw = WindowWidth(0)
            nh = WindowHeight(0)
            SetWindowLongPtr_(WindowID(0), #GWL_STYLE, GetWindowLongPtr_(WindowID(0), #GWL_STYLE)&~ #WS_THICKFRAME &~ #WS_DLGFRAME)
            ResizeWindow(0,0,0,DesktopWidth(0),DesktopHeight(0))
          
          Case 6
            SetWindowLongPtr_(WindowID(0), #GWL_STYLE, GetWindowLongPtr_(WindowID(0), #GWL_STYLE)|#WS_THICKFRAME | #WS_DLGFRAME)
            ResizeWindow(0,nx,ny,nw,nh)
          
          Case 7
            End                     
        EndSelect
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 1            
          EndSelect          

  EndSelect 

Until Quit = 1
End
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

Hi Rashad.
I wasn’t asking for — even less, expecting — new DPi-enabled code. I’ve got that aspect of things all under control, after studying and understanding your original response.
But what I was very curious about whether you have a display that requires the “enable DPi” option be set in order to get satisfactory results in front of your eyes.

Thank you just the same for the new code. I’ll compare that with my own fabrications and try to determine if there’s a chance that you may eventually become as good as me :D :lol:
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by RASHAD »

I am trying hard to come closer
Be aware :D
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

ERASED (accidental duplicate)
Last edited by Blue on Fri Mar 31, 2023 6:03 pm, edited 1 time in total.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

And a final clarification (if you don't mind my indiscretion) :
does your usual display equipment require DPi conformity ( and contorsions !!! ) in order to achieve correct results ?
My current laptop is an Asus equipped with a 16-in OLED display with a native resolution of 3840 X 2400 sporting a 250% desktop scale. It requires a lot of code fiddling to display satisfactorily.
Last edited by Blue on Fri Mar 31, 2023 6:26 pm, edited 1 time in total.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4635
Joined: Sun Apr 12, 2009 6:27 am

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by RASHAD »

Oh Blue
Your display is 4K UHD
Mine is BenQ 1920x1080 FHD
Yes if I changed to higher scale I need the DPI indeed
Egypt my love
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by Blue »

Thank you Rashad.
Your Dpi-enabled code works just fine.

And it is interesting. Using DPi variables rather than the built-in DesktopScaled X() and Y() functions makes for a less wordy and cluttered source code. i like that.

Thanks for the ideas.

——————————————————————
You've shown over time that you know the Windows API pretty intimately.
Can you suggest where one should start looking for functions related to displaying graphics on the screen ? What I mean, is what would be adequate search terms ? Windows (the system) does a pretty impressive job of diplaying images on the desktop (centred, full, expanded, etc.) It must rely on some pretty solid and nifty canned API functions that I'd like to leverage, but I haven't a clue where to start looking.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
idle
Always Here
Always Here
Posts: 5039
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Resizing an image to fullscreen (à la Windows Desktop)

Post by idle »

I think it's time for some wrapper macros to handle this dpi stuff, we shouldn't need to do anything apart from setting a flag.
That's quiet a high resolution for a small screen. Mines 32 inch 3200x1400 I can still see it just but I often zoom the ide to make it a bit easier.
Post Reply