Get dimensions of Icon

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Get dimensions of Icon

Post by jacdelad »

Hi, I want to get the dimensions of an icon. I need this for a DLL, the icon is given to the DLL as a handle. Usually I would use GetObject_(), but that does not work with icons (does it?). How do I do this?

Code: Select all

;bitm=LoadImage(1,"D:\PureBasic\Ribbon\Icon1.ico");Icon, please change to a valid icon
bitm=CreateImage(1,32,32);Bitmap
Debug "Handle: "+Str(bitm)
Define temp.bitmap
Debug "Should be 32: "+Str(GetObject_(bitm,SizeOf(temp),@temp))
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: Get dimensions of Icon

Post by firace »

I have this little procedure but it gets the size from an icon file not handle (from my code library - not thoroughly tested)

Code: Select all

Procedure IconSize(filename$)   
  If ReadFile(0,filename$) : ReadData(0, *ByteArray, 2048)  :  EndIf  
  ProcedureReturn PeekA(*ByteArray + $06)
EndProcedure

i  = IconSize(iconfilepath$)
Debug "Size: " + i + "x" + i

User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Get dimensions of Icon

Post by jacdelad »

Hello firace,
thanks a lot, but this doesn't help. The DLL just gets the handle and the calling program decides where to get the icon from, so I unfortunately can't use that.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4622
Joined: Sun Apr 12, 2009 6:27 am

Re: Get dimensions of Icon

Post by RASHAD »

If you got the Handle of the Icon

Code: Select all

GetIconInfo_(hIcon, iconinfo.ICONINFO)
GetObject_(iconinfo\hbmMask, SizeOf(BITMAP), bitmap.BITMAP)

width = bitmap\bmWidth
height = bitmap\bmHeight
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Get dimensions of Icon

Post by jacdelad »

Ah thanks, great. I didn't know the API GetIconInfo_().

BTW, is there a safe way to determine whether my handle is an icon or image, other than trying GetObject_ and when failing doing RASHAD's method?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4622
Joined: Sun Apr 12, 2009 6:27 am

Re: Get dimensions of Icon

Post by RASHAD »

Code: Select all

bitH = LoadImage_(0,GetTemporaryDirectory()+"test.xxx",#IMAGE_BITMAP,16,16,#LR_LOADFROMFILE	)
If bitH
  Debug "Bitmap"
Else
  iconH = LoadImage_(0,GetTemporaryDirectory()+"test.xxx",#IMAGE_ICON	,16,16,#LR_LOADFROMFILE	)
  If iconH
    Debug "Icon"
  EndIf
EndIf
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Get dimensions of Icon

Post by jacdelad »

Can't do that, the image is already loaded and the handle is given to my dll.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4622
Joined: Sun Apr 12, 2009 6:27 am

Re: Get dimensions of Icon

Post by RASHAD »

Then use CopyImage_() with the Handle instead of LoadImage_()
Egypt my love
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Get dimensions of Icon

Post by jacdelad »

...then it's still easier to

Code: Select all

Define dimensions.bitmap,icon.iconinfo
If Not GetObject_(image,SizeOf(dimensions),@dimensions)
  GetIconInfo_(image,icon)
  GetObject_(icon\hbmMask,SizeOf(dimensions),@dimensions)
EndIf
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply