Page 1 of 1

imageGadget and loadImage size problems

Posted: Fri Aug 04, 2023 5:53 am
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)

Re: imageGadget and loadImage size problems

Posted: Fri Aug 04, 2023 6:40 am
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

Re: imageGadget and loadImage size problems

Posted: Fri Aug 04, 2023 4:56 pm
by TRS-Eric
Ah yes I see, DesktopScaledX and DesktopScaledY does look like the best way to do this. Thank you!

Re: imageGadget and loadImage size problems

Posted: Fri Aug 04, 2023 6:02 pm
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

Re: imageGadget and loadImage size problems

Posted: Fri Aug 04, 2023 7:34 pm
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: