CopyImage_ doesn't resize image

Just starting out? Need help? Post your questions and find answers here.
MinerDac
User
User
Posts: 11
Joined: Sat Jul 13, 2024 12:02 pm

CopyImage_ doesn't resize image

Post by MinerDac »

I used the post at > https://www.purebasic.fr/english/viewto ... daa2f60adb > the code by RASHAD to derive the below:

Code: Select all


UsePNGImageDecoder()

#IMAGE_ICON = 1

CatchImage(100, ?myimage)

pcon.ICONINFO
pcon\fIcon = #True
pcon\hbmMask = ImageID(100)
pcon\hbmColor = ImageID(100)
hIcon = CreateIconIndirect_(pcon) 
hIcon = CopyImage_(hIcon,#IMAGE_ICON,32,16,#LR_COPYDELETEORG)  ;Change size to 32 x 16
FreeImage(101)

< begin other window making/display code here ... If OpenWindow(0, ....etc...>

ImageGadget(1,100,20,32,16,hIcon)

<end other window making/display code here>

DataSection
  myimage: : IncludeBinary "D:\Test\myimage.png" ; where myimage.png original size is 64 x 32
EndDataSection
The 'icon' is displayed on the button but the image is not resized to 32 x 16 but rather is made square which ruins its 'aspect ratio' look.

To experiment, I also tried to set it as the window icon with this:

Code: Select all

<...same other code here for structure, window, etc.. except I changed to this to experiment and didn't use the button...> 
hIcon = CopyImage_(hIcon,#IMAGE_ICON,16,16,#LR_COPYDELETEORG)  ;Change size to 16 x 16

SendMessage_(WindowID(0), #WM_SETICON, 0, hIcon)
...and it set the window icon fine but its size is stretched vertically when it should have been 16 x 16 which is square so it appeared as a rectangle instead of square

What am I doing wrong to resize this image properly? Am I doing this wrong?

(Windows 10 pro system, PureBasic 6.11 LTS (Windows - x64) )
Last edited by MinerDac on Tue Jul 30, 2024 12:57 pm, edited 4 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: CopyImage_ doesn't resize image

Post by RASHAD »

Hi MinerDac
You must use the proper Image Decoder
In your case PNG image decoder

Code: Select all

UsePNGImageDecoder()
Egypt my love
MinerDac
User
User
Posts: 11
Joined: Sat Jul 13, 2024 12:02 pm

Re: CopyImage_ doesn't resize image

Post by MinerDac »

RASHAD wrote: Tue Jul 30, 2024 12:50 pm Hi MinerDac
You must use the proper Image Decoder
In your case PNG image decoder

Code: Select all

UsePNGImageDecoder()
I did use UsePNGImageDecoder() (I just didn't show it in the post ... edited to show it, sorry bout that, didn't copy n paste correctly)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: CopyImage_ doesn't resize image

Post by RASHAD »

Hi again

Code: Select all

UsePNGImageDecoder()

#IMAGE_ICON = 1

CatchImage(100, ?myimage)

pcon.ICONINFO
pcon\fIcon = #True
pcon\hbmMask = ImageID(100)
pcon\hbmColor = ImageID(100)
hIcon = CreateIconIndirect_(pcon) 
hIcon = CopyImage_(hIcon,#IMAGE_ICON,32,16,#LR_COPYDELETEORG)  ;Change size to 32 x 16

OpenWindow(0, 0, 0, ImageWidth(100)+20,ImageHeight(100)+20, "",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
FreeImage(100)
ImageGadget(1,100,20,32,16,hIcon)
Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit = 1

;Use your own IMAGE
DataSection
  myimage: : IncludeBinary "c:\temp\test.png" ; where myimage.png original size is 64 x 32
EndDataSection
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: CopyImage_ doesn't resize image

Post by RASHAD »

Resize the image can be done using ResizeImage()
CreateIconIndirect_() is to create icon with transparent background if needed
See next

Code: Select all

UsePNGImageDecoder()

#IMAGE_ICON = 1

CatchImage(100, ?myimage)
ResizeImage(100,64,64)

pcon.ICONINFO
pcon\fIcon = #True
pcon\hbmMask = ImageID(100)
pcon\hbmColor = ImageID(100)
hIcon = CreateIconIndirect_(pcon) 
;hIcon = CopyImage_(hIcon,#IMAGE_ICON,32,16,#LR_COPYDELETEORG)  ;Change size to 32 x 16

OpenWindow(0, 0, 0, 800,600, "",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
FreeImage(100)
ImageGadget(1,100,20,64,64,hIcon)
Repeat
  Select WaitWindowEvent(1)
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit = 1

;Use your own IMAGE
DataSection
  myimage: : IncludeBinary "c:\temp\test.png" ; where myimage.png original size is 64 x 32
EndDataSection
Egypt my love
MinerDac
User
User
Posts: 11
Joined: Sat Jul 13, 2024 12:02 pm

Re: CopyImage_ doesn't resize image

Post by MinerDac »

Ah HA! 😀

Thanks RASHAD.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: CopyImage_ doesn't resize image

Post by boddhi »

Hi,

Is there a reason to not use native PB commands?

Code: Select all

;UsePNGImageDecoder()

Define.f Ratio
Define.u ImgWidth,ImgHeight
LoadImage(101,#PB_Compiler_Home+"Examples\Sources\Data\Map.bmp")
CopyImage(101,100)
; FreeImage(101)
ImgWidth=ImageWidth(100):ImgHeight=ImageHeight(100)
If ImgWidth>32 Or ImgHeight>16
  If ImgWidth>ImgHeight
    Ratio=32/ImgWidth
  Else
    Ratio=16/ImgHeight
  EndIf
  ;ResizeImage(100,ImgWidth*Ratio*DesktopScaledX(100)/100,ImgHeight*Ratio*DesktopScaledY(100)/100)
  ResizeImage(100,ImgWidth*Ratio*DesktopScaledX(100)/100,ImgHeight*Ratio*DesktopScaledY(100)/100,#PB_Image_Smooth)
EndIf

OpenWindow(0,0,0,100,100,"Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
ImageGadget(1,34,42,32,16,ImageID(100))
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
 
Here we used two images (IDs 100 and 101), but we could directly resize the original image if we don't need to keep it in memory as it was originally.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply