imageGadget and loadImage size problems

Just starting out? Need help? Post your questions and find answers here.
TRS-Eric
User
User
Posts: 16
Joined: Thu Apr 23, 2020 7:42 pm

imageGadget and loadImage size problems

Post by TRS-Eric »

Hello,


I don't seem to be getting the correct size with an image when trying to use an imageGadget.
I'm on Windows 11 using 150% scale.
I'm using Compiler: 6.03 beta 3
I do have "Enable dpi aware executable" selected.

The imageGadget seems to be the right size when the image is not loaded, so I think imageGadget is ok.

But the image itself is not the correct size even when feeding it the WindowWidth.

Code: Select all

  loading_img = LoadImage(#PB_Any, "themes/loading.png")
  loading_img2 = ResizeImage(loading_img, WindowWidth(win_loading,#PB_Window_FrameCoordinate),WindowHeight(win_loading,#PB_Window_FrameCoordinate))
  Debug loading_img2
  ImageGadget(#PB_Any, 0,0,WindowWidth(win_loading,#PB_Window_FrameCoordinate),WindowHeight(win_loading,#PB_Window_FrameCoordinate),loading_img2,#PB_Image_Border)
I thought maybe if I load the gadget and then get the gadget width, it will give me the DPI adjusted width, but that's not working either. I need a way to get the DPI adjusted width and height so I can properly display an image in the imageGadget.

alternative example

Code: Select all

  loading_img = LoadImage(#PB_Any, "themes/loading.png")
  Debug loading_img2
 ;ImageGadget(#PB_Any, 0,0,WindowWidth(win_loading,#PB_Window_FrameCoordinate),WindowHeight(win_loading,#PB_Window_FrameCoordinate),loading_img2,#PB_Image_Border)
  loading_img_gadget = ImageGadget(#PB_Any, 0,0,390,WindowHeight(win_loading,#PB_Window_FrameCoordinate),0,#PB_Image_Border)
  loading_img2 = ResizeImage(loading_img, GadgetWidth(loading_img_gadget,),GadgetHeight(loading_img_gadget))
  SetGadgetState(loading_img_gadget, loading_img2)

// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)
---
BASIC related discord: https://discord.gg/KS4en5y5j4
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: imageGadget and loadImage size problems

Post by Little John »

// Code improved according to netmaestro's hint

Hi,

the problem at hand is, that windows and gadgets are scaled automatically according to the DPI value of your system, but images are not. That's by design, and not a bug in PureBasic. Please ask your coding questions in the "Coding questions" section of the forum.

Here is one way to show Window, ImageGadget and Image with proper sizes:

Code: Select all

EnableExplicit

Define.i img, width, height

UseJPEGImageDecoder()

img = LoadImage(#PB_Any, "<path><myimage>.jpg")
If img = 0
   MessageRequester("Error", "Can't load image.")
EndIf

width  = DesktopUnscaledX(ImageWidth(img))
height = DesktopUnscaledY(ImageHeight(img))

If OpenWindow(0, 0, 0, width+10, height+10, "ImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
   MessageRequester("Fatal error", "Can't open main window.")
   End
EndIf

ImageGadget(0, 5,5, 0,0, ImageID(img))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
 
 
This is another way:

Code: Select all

EnableExplicit

Define.i img, width, height

UseJPEGImageDecoder()

img = LoadImage(#PB_Any, "<path><myimage>.jpg")
If img = 0
   MessageRequester("Error", "Can't load image.")
EndIf

width  = ImageWidth(img)
height = ImageHeight(img)

If OpenWindow(0, 0, 0, width+10, height+10, "ImageGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
   MessageRequester("Fatal error", "Can't open main window.")
   End
EndIf

ResizeImage(img, DesktopScaledX(width), DesktopScaledY(height))
ImageGadget(0, 5,5, 0,0, ImageID(img))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by Little John on Tue Aug 08, 2023 9:36 am, edited 1 time in total.
TRS-Eric
User
User
Posts: 16
Joined: Thu Apr 23, 2020 7:42 pm

Re: imageGadget and loadImage size problems

Post by TRS-Eric »

Ah yes I see, DesktopScaledX and DesktopScaledY does look like the best way to do this. Thank you!
---
BASIC related discord: https://discord.gg/KS4en5y5j4
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: imageGadget and loadImage size problems

Post by netmaestro »

Try just using 0 and 0 for the width and height of the imagegadget. It's the correct usage unless you don't have an image to put in there and you just want to reserve the screen real estate. If you do have an image, use 0's
BERESHEIT
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: imageGadget and loadImage size problems

Post by Little John »

netmaestro wrote: Fri Aug 04, 2023 6:02 pm Try just using 0 and 0 for the width and height of the imagegadget. It's the correct usage unless you don't have an image to put in there and you just want to reserve the screen real estate. If you do have an image, use 0's
Cool! 8)
I didn't know that ... Although I see now that it's written in the help. :oops:
Post Reply