Get dimensions of Icon

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

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))
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
firace
Addict
Addict
Posts: 947
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: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
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: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
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: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
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: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Re: Get dimensions of Icon

Post by mestnyi »

how can I get the actual cursor size?
Post Reply