There appears to be a problem with CopyImage if the image you are trying to copy has been used as a gadget item image.
It crashes with "Invalid memory access" at the end of the program. Works fine on GTK.
Code:
EnableExplicit
Enumeration
#Window
#Tree
#Canvas
EndEnumeration
Define img, icon
img = CreateImage(#PB_Any, 16, 16, 24)
If StartDrawing(ImageOutput(img))
Box(0, 0, OutputWidth(), OutputHeight(), RGB(0, 0, 255))
StopDrawing()
EndIf
OpenWindow(#Window, 0, 0, 320, 240, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TreeGadget(#Tree, 10, 10, 100, 200)
AddGadgetItem(#Tree, -1, "Item 1", ImageID(img))
icon = CopyImage(img, #PB_Any)
CanvasGadget(#Canvas, 120, 10, 50, 50)
If StartDrawing(CanvasOutput(#Canvas))
DrawImage(ImageID(icon), 10, 10)
StopDrawing()
EndIf
FreeImage(icon)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow