Convert loaded PNG image to icon in memory?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Convert loaded PNG image to icon in memory?

Post by Fangbeast »

Several programs use PNG images for various reasons and if I have to use the same PNG's somewhere else at the same time, easy.

But, I have a balloon tooltip routine cobbled together from the forum using API but it requires ICO format files. It uses the exact same images as above but I want to save memory by copying the png resource and converting to ICO on the fly for the below command.

CopyImage_(ImageID(Image.i), #IMAGE_ICON, 48, 48, 0).

Anyone know if that's possible?
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Convert loaded PNG image to icon in memory?

Post by RASHAD »

Hi Fangles

Code: Select all

  UsePNGImageDecoder()
  LoadImage(0,"g:\girl.png")
  LoadImage(1,"g:\transparent1.png")
  
  iinf.ICONINFO
  iinf\fIcon = 1
  
  OpenWindow(1,0,0,400,300,"Icontest",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
  

  iinf\hbmMask = ImageID(0)
	iinf\hbmColor = ImageID(0)
	icoHnd_0 = CreateIconIndirect_(iinf)             ;Create Icon in memory
	FreeImage(0)
	icoHnd_0 = CopyImage_(icoHnd_0,#IMAGE_ICON,48,48,#LR_COPYDELETEORG)  ;Change size of icon to 48 x 48
	ImageGadget(0,10,10,48,48,icoHnd_0) 
	
	iinf\hbmMask = ImageID(1)
	iinf\hbmColor = ImageID(1)
	icoHnd_1 = CreateIconIndirect_(iinf)
	FreeImage(1)
	icoHnd_1 = CopyImage_(icoHnd_1,#IMAGE_ICON,48,48,#LR_COPYDELETEORG)
  ImageGadget(1,70,10,48,48,icoHnd_1)
  
  ButtonGadget(2,10,265,80,25,"Change Icon")
    Repeat
      event=WaitWindowEvent()
      
       If event=#PB_Event_Gadget
         Select EventGadget()
         Case 2
             Run ! 1
             If Run = 0
                 SendMessage_(WindowID(1),#WM_SETICON,#False, icoHnd_0)
             Else
                 SendMessage_(WindowID(1),#WM_SETICON,#False, icoHnd_1)
             EndIf
         EndSelect
       EndIf
      
    Until event=#WM_CLOSE

DataSection
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert loaded PNG image to icon in memory?

Post by Fangbeast »

RASHAD!!! Yoi dead-set legend!! I bow to you and your mighty brain!

Now, let's see me understand what you just did :):):)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Convert loaded PNG image to icon in memory?

Post by Bisonte »

Rashad's code with procedures and some comments...
Images from : http://www.fatcow.com/free-icons

Code: Select all

UsePNGImageDecoder()

CatchImage(0,?action_log_png_start)
CatchImage(1,?abacus_png_start)

Procedure PngToIco(Image) ; Convert an image to an ico. Returns the icon handle. To free it use : DestroyIcon_(icoHnd)
  
  If Not IsImage(Image) : ProcedureReturn #False : EndIf
  
  Protected iinf.ICONINFO
  Protected icoHnd
  
  ; Fill the ICONINFO structure
  iinf\fIcon = 1
  iinf\hbmMask = ImageID(Image)
  iinf\hbmColor = ImageID(Image)
  
  ; Create the icon in memory
  icoHnd = CreateIconIndirect_(iinf)
  
  ProcedureReturn icoHnd
  
EndProcedure
Procedure ChangeIconSize(icoHnd, Size) ; Change size of icon To Size x Size
  
  If icoHnd
    icoHnd = CopyImage_(icoHnd, #IMAGE_ICON, Size, Size, #LR_COPYDELETEORG)
  EndIf
  ProcedureReturn icoHnd
  
EndProcedure

OpenWindow(1,0,0,400,300,"Icontest",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

icoHnd_0 = PngToIco(0) ; Convert Image 0 to an icon
icoHnd_0 = ChangeIconSize(icoHnd_0, 48)  ;Change size of icon to 48 x 48
FreeImage(0)
ImageGadget(0,10,10,48,48,icoHnd_0) ; Display Icon

icoHnd_1 = PngToIco(1); Convert Image 1 to an icon
icoHnd_1 = ChangeIconSize(icoHnd_1, 64) ; Change size of icon to 64 x 64
FreeImage(1)
ImageGadget(1,70,10,64,64,icoHnd_1)

ButtonGadget(2,10,265,80,25,"Change Icon")
Repeat
  event=WaitWindowEvent()
  
  If event=#PB_Event_Gadget
    Select EventGadget()
      Case 2
        Run ! 1
        If Run = 0
          SendMessage_(WindowID(1),#WM_SETICON,#False, icoHnd_0) ; Set the window icon
        Else
          SendMessage_(WindowID(1),#WM_SETICON,#False, icoHnd_1) ; Set the window icon
        EndIf
    EndSelect
  EndIf
  
Until event=#PB_Event_CloseWindow

; Free the icons, cause they NOT free by PB
DestroyIcon_(icoHnd_0)
DestroyIcon_(icoHnd_1)

DataSection
  action_log_png_start: ;{ Datas : Size = 680 Bytes
  Data.q $0A1A0A0D474E5089,$524448490D000000,$1000000010000000,$FFF31F0000000608,$5845741900000061
  Data.q $72617774666F5374,$2065626F64410065,$6165526567616D49,$00003C65C9717964,$DA78544144494402
  Data.q $C61461544BCD939C,$1D4DE238CEFBF77F,$16DA22E1485C8A51,$2FEC122D01FD0516,$B516826D11688428
  Data.q $68CB41B708BAD369,$84BB5F4182859453,$471263042B020C14,$B7BEFBDEE7733BC7,$1C0F08B410667573
  Data.q $DCE73CF39EFBCBEE,$34F5E9BDC8E31CE7,$8667FF1919D681D3,$9BB55986D81A69B8,$D6DF234FC7E3074A
  Data.q $3C01CB06FF673CAB,$A51D818FD7E9E4CB,$DC9EC41C072E8F19,$8A020E5545AAD8D1,$5C5C75C1C76C6EA5
  Data.q $D86984C63D74C5CF,$AF4298F8FAE67832,$9950EFF7DBC27151,$F7249758B25DB1B4,$3800BE02FCF197F6
  Data.q $D7299A8C79E64B96,$31A4944343E990D3,$76154ECD8DC26B4B,$B88EC92BB048E0FB,$65C556342648166D
  Data.q $92268E361DB193F8,$00EA288D3E7B3E02,$A0E012396243ED98,$21AB85091A0F028D,$CF61E6334CE4C86A
  Data.q $306DA95C6B9B9320,$EEF2DCB216215955,$0965C92E5EDFF6ED,$9082A27A6C854E96,$41EE99B013E80168
  Data.q $574FB74DE2FEADAE,$36C3D1D4BCB33192,$22D78C0DBFA3085A,$CFD536314804768F,$F8BE594B50EB4BF7
  Data.q $68CC26F70A7D5B8D,$769C96C587351B8B,$0F73F0E687E4F05B,$E8BFA90F1D51E6BF,$D73A818EA2AE4D41
  Data.q $B84AA87E68733660,$111D9B07BB4A49A6,$9E91281E53E13818,$A564D15E2F71FF05,$51F6F271EFB899A0
  Data.q $87AF5119B89A9FCC,$A3A129D85812D654,$8BE614C4E44C8882,$B824139F993E8BF8,$12BA5499AF28472F
  Data.q $BB543A073250AD94,$C27108DEA1E9DC65,$33688AAA4C470CAA,$3562CE7148B677A1,$1521A8A2224A22DB
  Data.q $D3B60F7690C645D4,$7AF3FECBE17E7212,$BC7EDAE79CA3EFE1,$FBD13E757DDC1735,$1B24F67B6BB64FAC
  Data.q $B6C1F888D3F9B9E4,$B3D10018047FAA09,$0000606BCC9BDEE0,$42AE444E45490000,$0000000000008260
  action_log_png_eof: ;}
EndDataSection

DataSection
  abacus_png_start: ;{ Datas : Size = 1592 Bytes
  Data.q $0A1A0A0D474E5089,$524448490D000000,$2000000020000000,$7A7A730000000608,$58457419000000F4
  Data.q $72617774666F5374,$2065626F64410065,$6165526567616D49,$00003C65C9717964,$DA7854414449D505
  Data.q $5D1455156C5B57C4,$40A052D2DBDCCCE7,$6212062240A8285B,$1A3FA82D40AB0F22,$47C18A5B10415235
  Data.q $80210D0D12106982,$4D0FC621B518C484,$261F8948B01A9684,$E9FA36F29685887E,$7A3EF6DBDA54A287
  Data.q $99F7BDC66771FBE9,$C9BDA9884D0A5B96,$ECEBDECF67399EE4,$4C3CF3CA333ED6BD,$400E137F82634FE4
  Data.q $7FB870E0FA2815D1,$2C394FFFB8EA99C0,$A8FB1D6EB41290AD,$EDFE0E74FBA6CB34,$A95069DDD7D03BA7
  Data.q $0CF4D4B7443C09F1,$0066C72A7CB3BABC,$59A9B1A16571C741,$2CE784C04271A9D0,$5B4198FC08F85C2F
  Data.q $79385B4045D96416,$ED1632D086002A2D,$BA5D8C3FEA076203,$6D8A86B1219B2FD6,$29B1A5D165A4608F
  Data.q $F548CE07CFAE1B2F,$EB9E07227B5E28D5,$D8FFDFAD3095C4E2,$0F71891685D571A5,$0E621160A8B4B815
  Data.q $F43D00B9A2CBFDAA,$DB1A6C0408BDFEE1,$8CA0C1823F607838,$038CC04D8635BCCD,$AC958E0C4B5AD801
  Data.q $2A9EB49FA575EC82,$B72F27087373452F,$BDF35B58C497B4C9,$49A7DD20AC063496,$6A520EE122F45C86
  Data.q $36C374A60F5FB05D,$B9AA010CA8727B1E,$C54C570A1744FE83,$351871DC28D9DCEC,$E9C7812EDCE57787
  Data.q $D8DD2E5578BC12D0,$9BEB2A2E519A0194,$95DDF1A2B4598D28,$A8964793CCE6FC1E,$E6F8BA829D3F34EB
  Data.q $6DBA949953360B62,$624BF0C7665BBBC3,$D9FC93FEB179ECBA,$36D1407047DA9240,$B06565658D28B2F8
  Data.q $52F9E8B76A8A0B6C,$6DC2A04500096302,$58CCAD7B21CAB006,$5B85D2E584CBD243,$B398D088D1CF027C
  Data.q $647B3FC9F3CDB3A1,$60B435546B1DE083,$E50BBC03253B4BC9,$41519427DABA9C6A,$FE9DA889882CCF0B
  Data.q $1AA549A0AB79DA76,$7B44C8324C6BFF7F,$78DD784FB10DF676,$22307BAF94876E6E,$904A90130027BD25
  Data.q $8FBEF11C5B634B9F,$E6308E6FB571C7D7,$0E4A5BEF1476F4B1,$9F1F8A2CF7B57C1D,$438C2D7D38254503
  Data.q $50ABCBD94FC41BA7,$752F271A36419798,$6ED47F9914EF52E1,$3E9C8E993B02A51C,$A45DAEE77A332DD6
  Data.q $A200F0BCB1584B00,$4DC2BA3532A36351,$5A3B865680C75294,$686D39FDDA878859,$21FB0B687421FC4C
  Data.q $93019AEEA29EFD72,$1696335D65514E3A,$B106F139C8486FBE,$60DF60F4786EA1EE,$083BDC83FF3FAA07
  Data.q $A640487CEAA3F40C,$53C45B1D8BE12705,$61146431951740CF,$06C94A6E07B9E0A7,$EC08C91939F21E9C
  Data.q $FFA35243C8400521,$424A602604B98292,$BA675D43175E39BB,$BE6116C21E915C38,$15CFE49446BA8A83
  Data.q $88E78DA89D8BEA9B,$9654F2D84D6915BB,$7FCCDF39B41583A8,$4AC352AD2196EE7D,$C01D96849F02A918
  Data.q $BBC286A6C3B777F6,$987361C210F1B95B,$CD5334B4ACDCC9E8,$DAD06583A8B4B10C,$0403E4F34F9A3BE6
  Data.q $A064BB25B47AB152,$3A12C82E93C0A863,$AC33DC6DE41FF67A,$F16FCC2F07B74182,$26821D0B89C09753
  Data.q $5393CB9518E2A768,$358E36302E2F0525,$9D4C1AF1316BCA88,$808A56F632E9D94C,$DDA21770F6046E4F
  Data.q $A41E6B1BD11A6D68,$B99E55AC831B9B5B,$1F389D5916C8CA4E,$94D56A796CADFD47,$8AC4F702CFE51456
  Data.q $7B764B30545137B3,$9E4E5EEFAF044AE4,$D25BEA3BFC7CD42C,$B6C442D1993EFBA4,$33D0D3A48810D625
  Data.q $EB79DE36D91AE63B,$1ED36F2C4B01294D,$BF1123D78E0232EE,$7111A618A4CA9964,$0913D681F0596313
  Data.q $51A2CF6CD899394B,$4DDDE58E1AF494D3,$606F6B2A8C3FCA05,$1A89C8A1913ED175,$850728D8DC437551
  Data.q $F4717AF1C2E2230B,$D4FD93A549B10DDC,$F8A9D7485BAD10E5,$FFFEA7EFB1A758E9,$8026E89039085470
  Data.q $12D1A3E182A2EED3,$E6A8C23F99AF15A9,$35C2429922671C42,$689CBA7ABF58A333,$6279DAC9475F3F15
  Data.q $147E2BBB09B1735A,$14A687E236DBE500,$606C31198280B03A,$DD1A5C4CE470027B,$BF5499D212A13B3D
  Data.q $E9E0A8C2552B6DBD,$A96552FD8488E5AD,$162E2FFB4A94AF91,$B70F6CCF3CA8A162,$6F9EA643198FFE9A
  Data.q $550F430E6059C63E,$812A41EB60A37F75,$DC5FAC4E8903A896,$1B595266A41BB52E,$92FF6A2A62B1E217
  Data.q $C56597D7074299EE,$468B2BC0EBEAEE32,$FA1000318B769BDB,$2FF47FA4460BB592,$51BDED9304DF3EF3
  Data.q $D4DE3BE96D8F05BE,$931FB9DFC02BF3C7,$DE1B0D3D03B9E033,$FBC754DF82656CFB,$C72639C1C44A8D53
  Data.q $CCC1E75174194866,$EE2E825FB71925D1,$17BF893A0FCA8D26,$86FC0C83B8FF39C6,$09BA24869FD13BD3
  Data.q $2100018057FF3CFE,$00147779DA843D47,$AE444E4549000000,$0000000000826042
  abacus_png_eof: ;}
EndDataSection
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert loaded PNG image to icon in memory?

Post by Fangbeast »

RASHAD, I don't understand some things and I need to. Looking at the API, I can see that:

Code: Select all

iinf\hbmMask = ImageID(0)
is "The icon bitmask bitmap" of the original image

Code: Select all

iinf\hbmColor = ImageID(0)
is "A handle to the icon color bitmap" of the original image

Code: Select all

icoHnd_0 = CreateIconIndirect_(iinf)
Creates an icon in memory with the attributes above and gives us a handle to it and I am assuming that it's blank because nowhere have we said the rest of the ICONINFO structure is filled with the details of the original loaded PNG image.

Code: Select all

FreeImage(0)
But now, we have freed the original image, so where are we getting the icon data from?

Code: Select all

icoHnd_0 = CopyImage_(icoHnd_0, #IMAGE_ICON, 48, 48, #LR_COPYDELETEORG)
This should be copying blank data because we freed the original image, but it doesn't??

Oh, my aching head.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert loaded PNG image to icon in memory?

Post by Fangbeast »

Bisonte, how did you create those image datasections?
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Convert loaded PNG image to icon in memory?

Post by Paul »

Fangbeast wrote:RASHAD, I don't understand some things and I need to. Looking at the API, I can see that:

Code: Select all

FreeImage(0)
But now, we have freed the original image, so where are we getting the icon data from?

Code: Select all

icoHnd_0 = CopyImage_(icoHnd_0, #IMAGE_ICON, 48, 48, #LR_COPYDELETEORG)
This should be copying blank data because we freed the original image, but it doesn't??

Oh, my aching head.
FreeImage() is a PB command and releases the image that was allocated in memory using CatchImage
After Catching the image you have converted it using API so the image now exists elsewhere in memory and is accessed using a Windows Handle.
If you look at Bisonte's code you will see he actually frees the icon images from memory before exiting using DestroyIcon_(icoHnd_0) and DestroyIcon_(icoHnd_1)
By doing this, NOW the images are no longer accessible.
Image Image
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Convert loaded PNG image to icon in memory?

Post by Bisonte »

Fangbeast wrote:Bisonte, how did you create those image datasections?
I use an own modified version of StarGate "The Legend" 's Bin2Data - IDE Tool...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Convert loaded PNG image to icon in memory?

Post by RASHAD »

Hi Fangles

1- LoadImage(0,"g:\girl.png") [we have now an image loaded in memory]
.
.
.
2- CreateIconIndirect_(iinf) [ we created an icon image from the previous image in some place else in memory]

3- FreeImage(0) [ we do not need the original image any more so we freed the image from memory]

4- CopyImage_(icoHnd_1,#IMAGE_ICON,48,48,#LR_COPYDELETEORG)
[ we created another icon image with the new size in some place else in memry and at the same time we freed the original icon image from memory by using the flag #LR_COPYDELETEORG]

That means we still have only one icon image in memory

For bin2data beside StarGate reported by Bisonte

1- http://www.purebasic.fr/english/viewtop ... t=bin2data by ts-soft

2- http://www.purebasic.fr/english/viewtop ... t=bin2data by RASHAD :mrgreen:

Have fun
Egypt my love
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Convert loaded PNG image to icon in memory?

Post by Fangbeast »

RASHAD..

iinf\fIcon = 1
iinf\hbmMask = ImageID(Image)
iinf\hbmColor = ImageID(Image)
CreateIconIndirect_(iinf)

All you did was fill the iinf structure with the colour and mask and in this statement "CreateIconIndirect_(iinf)" you are referencing that structure but nowhere does it point to a handle of the original image for creating from but then you say "we created an icon image from the previous image in some place else in memory"

Where did iinf get filled with the handle of the loaded image so it knows where to create from? Magic?

I think I have to go lie down. Us old folks can't take any more.

@Paul, thanks for trying, this is as clear as mud to me. Some parts make sense, some parts look more like magic.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Convert loaded PNG image to icon in memory?

Post by kenmo »

Nice useful code, RASHAD and Bisonte.
Post Reply