Problem with GetGadgetAttribute and Image

Just starting out? Need help? Post your questions and find answers here.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Problem with GetGadgetAttribute and Image

Post by Paul »

Why does this work with #PB_Any and not with Constant ??

Code: Select all

OpenWindow(0,0,0,0,0,"")
i=CreateImage(#PB_Any,256,256,24)
ButtonImageGadget(0,0,0,256,256,i)

Debug ImageID(i)  
Debug ImageWidth(i)
img=GetGadgetAttribute(0,#PB_Button_Image)
Debug ImageID(img)
Debug ImageWidth(img)



CreateImage(0,256,256,24)
ButtonImageGadget(0,0,0,256,256,ImageID(0))
Debug ImageID(0)  
Debug ImageWidth(0)
img=GetGadgetAttribute(0,#PB_Button_Image)
Debug img
;Debug ImageWidth(img)   ;<--- this line causes error
Image Image
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Problem with GetGadgetAttribute and Image

Post by normeus »

so in your program using a constant createImage(2,256,256,24) does not return 2 as the "Number" you have to change your code to use either a number or a handle ( #PB_Any)

YOUR QUESTION:

your line:
ButtonImageGadget(0,0,0,256,256,ImageID(0))

should be:
ButtonImageGadget(0,0,0,256,256,0)


from the Help file

https://www.purebasic.com/documentation ... ndles.html

EDIT: figured out what the real question was.

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Problem with GetGadgetAttribute and Image

Post by Demivec »

Paul wrote:Why does this work with #PB_Any and not with Constant ??
First, coding questions should be posted in the 'Coding Questions' forum. This forum is for bugs that should be properly verified.

Second your code contains more than one error.

Code: Select all

OpenWindow(0, 0, 0, 0, 0, "")
i=CreateImage(#PB_Any, 256, 256, 24)
ButtonImageGadget(0, 0, 0, 256, 256, ImageID(i)) ;You should assign an ImageID to a ButtonImageGadget not the image#.

Debug i
Debug ImageID(i)  
Debug ImageWidth(i)
img = GetGadgetAttribute(0, #PB_Button_Image) ;This obtains the ImageID, not the image#
Debug img ;ImageID

Debug "-------"
result = CreateImage(0, 256, 256, 24)
ButtonImageGadget(0, 0, 0, 256, 256, ImageID(0))
Debug result
Debug ImageID(0)  
Debug ImageWidth(0)
img = GetGadgetAttribute(0, #PB_Button_Image) ;This obtains the ImageID, not the image#
Debug img  ;ImageID
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem with GetGadgetAttribute and Image

Post by Paul »

normeus wrote: your line:
ButtonImageGadget(0,0,0,256,256,ImageID(0)) ;<-- this is fine to use ImageID or Image Handle

should be:
ButtonImageGadget(0,0,0,256,256,0) ;<-- incorrect
You can use ImageID or Image Handle.

Demivec wrote: First, coding questions should be posted in the 'Coding Questions' forum. This forum is for bugs that should be properly verified.
Second your code contains more than one error.
You haven't pointed out any errors in the code, only the fact that some PB commands may be incomplete.
Commands like ImageWidth only work with the Image# and NOT an ImageID
Commands like GetGadgetAttribute(Gadget,#PB_Button_Image) only return ImageID making it not very useful.
I don't recall a PB command that can return an Image# from an ImageID, only an ImageID from an Image#.

If PB commands are suppose to integrate smoothly with each other and do not, then it is a bug (and only Fred can comment on this)... but if there are suppose to be these incompatibilities making some commands not very usable, then I will post this in the Feature Request section.

Of course any Mod is quite welcome to move this to the coding section until then ;)

Code: Select all

OpenWindow(0,10,10,525,300,"")

#Image=1
hImage=CreateImage(#Image,256,256,24,$FF0000)
ButtonImageGadget(0,0,0,256,256,hImage) 
ButtonImageGadget(1,260,0,256,256,ImageID(#Image)) 

Debug hImage
Debug ImageID(#Image)
Debug GetGadgetAttribute(1,#PB_Button_Image) 


Debug ImageWidth(#Image) 
;Debug ImageWidth(hImage) ;<-- ImageWidth cant use ImageID
;Debug ImageWidth(GetGadgetAttribute(1,#PB_Button_Image)) ;<--returns ImageID so unusable with ImageWidth

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Image Image
mestnyi
Addict
Addict
Posts: 1000
Joined: Mon Nov 25, 2013 6:41 am

Re: Problem with GetGadgetAttribute and Image

Post by mestnyi »

Paul wrote:
normeus wrote: your line:
ButtonImageGadget(0,0,0,256,256,ImageID(0)) ;<-- this is fine to use ImageID or Image Handle

should be:
ButtonImageGadget(0,0,0,256,256,0) ;<-- incorrect
You can use ImageID or Image Handle.

Demivec wrote: First, coding questions should be posted in the 'Coding Questions' forum. This forum is for bugs that should be properly verified.
Second your code contains more than one error.
You haven't pointed out any errors in the code, only the fact that some PB commands may be incomplete.
Commands like ImageWidth only work with the Image# and NOT an ImageID
Commands like GetGadgetAttribute(Gadget,#PB_Button_Image) only return ImageID making it not very useful.
I don't recall a PB command that can return an Image# from an ImageID, only an ImageID from an Image#.

If PB commands are suppose to integrate smoothly with each other and do not, then it is a bug (and only Fred can comment on this)... but if there are suppose to be these incompatibilities making some commands not very usable, then I will post this in the Feature Request section.

Of course any Mod is quite welcome to move this to the coding section until then ;)

Code: Select all

OpenWindow(0,10,10,525,300,"")

#Image=1
hImage=CreateImage(#Image,256,256,24,$FF0000)
ButtonImageGadget(0,0,0,256,256,hImage) 
ButtonImageGadget(1,260,0,256,256,ImageID(#Image)) 

Debug hImage
Debug ImageID(#Image)
Debug GetGadgetAttribute(1,#PB_Button_Image) 


Debug ImageWidth(#Image) 
;Debug ImageWidth(hImage) ;<-- ImageWidth cant use ImageID
;Debug ImageWidth(GetGadgetAttribute(1,#PB_Button_Image)) ;<--returns ImageID so unusable with ImageWidth

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
I totally agree with you. It seems that Fred never uses his creation. :(
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Problem with GetGadgetAttribute and Image

Post by Josh »

It's a feature that Pb just doesn't offer, so it belongs in the 'Feature Requests and Wishlists' section and not in 'Bugs'.

What I don't understand is that there is the function 'ImageId()' and no corresponding reverse function. This has certainly been discussed hundreds of times in this forum and is conscientiously ignored by Fred.

Code: Select all


Import ""
  PB_Image_Objects
  PB_Object_EnumerateStart (*object)
  PB_Object_EnumerateNext  (*object, *Id)
  PB_Object_EnumerateAbort (*object)
EndImport

Procedure.i GetImageFromHandle (hImage)
  Define nImage.i

 ;Objektliste durchlaufen
  PB_Object_EnumerateStart (PB_Image_Objects)
  While PB_Object_EnumerateNext (PB_Image_Objects, @nImage)
    If hImage = ImageID (nImage)
      PB_Object_EnumerateAbort (PB_Image_Objects)
      ProcedureReturn nImage
    EndIf
  Wend

 ;Kein Image gefunden
  ProcedureReturn -1

EndProcedure

OpenWindow(0,10,10,525,300,"")

#Image=1
hImage=CreateImage(#Image,256,256,24,$FF0000)
ButtonImageGadget(0,0,0,256,256,hImage) 
ButtonImageGadget(1,260,0,256,256,ImageID(#Image)) 

Debug hImage
Debug ImageID(#Image)
Debug GetGadgetAttribute(1,#PB_Button_Image) 


Debug ImageWidth(#Image) 
Debug ImageWidth(GetImageFromHandle(hImage))
;Debug ImageWidth(GetGadgetAttribute(1,#PB_Button_Image)) ;<--returns ImageID so unusable with ImageWidth

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Problem with GetGadgetAttribute and Image

Post by Demivec »

Paul wrote:
Demivec wrote: First, coding questions should be posted in the 'Coding Questions' forum. This forum is for bugs that should be properly verified.
Second your code contains more than one error.
You haven't pointed out any errors in the code, only the fact that some PB commands may be incomplete.
Commands like ImageWidth only work with the Image# and NOT an ImageID
Commands like GetGadgetAttribute(Gadget,#PB_Button_Image) only return ImageID making it not very useful.
I don't recall a PB command that can return an Image# from an ImageID, only an ImageID from an Image#.

If PB commands are suppose to integrate smoothly with each other and do not, then it is a bug (and only Fred can comment on this)... but if there are suppose to be these incompatibilities making some commands not very usable, then I will post this in the Feature Request section.

Of course any Mod is quite welcome to move this to the coding section until then ;)
I haven't pointed out PB commands are incomplete, they are as they are specified. You yourself state that ImageWidth works only with an Image# and not an ImageID but you are trying to make it do so nonetheless. That results in an error.

If you want to know the Image# for an ImageID then go through the Image#s that your program creates and compare their ImageID's to the one retrieved from the gadget. You can use a map, array or list to save them for the future or use whatever solution you like. You are a programmer, this isn't rocket science. :mrgreen:

First things second I suppose. Here are your silly errors.

Code: Select all

OpenWindow(0,0,0,512,512,"") ;let's enlarge the window so we can see something
i=CreateImage(#PB_Any,256,256,24)

;add this line to distinguish tbe image you created, make it red
StartDrawing(ImageOutput(i)):Box(0,0,256,256,RGB(255,0,0)):StopDrawing()


;Your original code line is in error it is assigning the image number not the imageID.
;Does it show a red button image, if no then the statement is not using the correct image.
ButtonImageGadget(0,0,0,256,256,i)
;Try this corrected line instead that has the parameters specified properly.
;ButtonImageGadget(0,0,0,256,256,ImageID(i)) ;remember to comment out the two problem debugs also.

Debug ImageID(i)  
Debug ImageWidth(i)
;The following lines only work with your incorrect ButtonImageGadget() line above.  This
;is because the assigned attribute, although incorrect is still retained for retrieval.
;The second debug statement won't work once the buttonimagegadget is correctly specified
img=GetGadgetAttribute(0,#PB_Button_Image)
;These two lines will cause errors if the ButtonImageGadget() is correctly specified because the
;retrieved attribute value 'img' will be an ImageID and not a image#.  They should be commented out.
Debug ImageID(img)
Debug ImageWidth(img)


;These lines function as they should and consistent with the previous lines once the previous
;ButtonImageGadget() is correctly specified.  Ones that cause errors should cause errors.
CreateImage(0,256,256,24)

;add this line to distinguish tbe image you created, make it green
StartDrawing(ImageOutput(0)):Box(0,0,256,256,RGB(0,255,0)):StopDrawing()

ButtonImageGadget(1,0,256,256,256,ImageID(0))
Debug ImageID(0)  
Debug ImageWidth(0)
img=GetGadgetAttribute(1,#PB_Button_Image)
Debug img
;Debug ImageWidth(img)   ;<--- this line causes error ;it should cause an error
Repeat: Until WaitWindowEvent(10) = #PB_Event_CloseWindow
Other important ideas from Please read before submitting a bug report !.

(3) Have you triple-checked your code? Sometimes we make the simplest of mistakes, and post "bugs" only to find out we forgot something.
(4) If something doesn't "seem to work", then post it in the Questions section first, unless you know it's a bug. A lot of times a bug is just lack of knowledge by a newbie of how to use a command correctly.
(5) Bugs, generally, are commands that don't work as they should, or commands that worked on an earlier version of PureBasic but don't on the newest. This means you shouldn't report things like the compiler crashing because you used a Return without Gosub.


Miscellaneous links to documentation from the Help manual:

CreateImage(), see return values.
ImageWidth(), see parameters list.
ButtonImageGadget(), see parameters list.
Handles and Numbers, see remarks about #PB_Any.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem with GetGadgetAttribute and Image

Post by Paul »

Thanks Demivec for posting your commented code and links, they will help new users to PB better understand some of its inconsistency due to its flexibility :)

Sorry my first post was not very good and I should not have added any of the #PB_Any stuff. I tried to clear things up a little better in my second post but failed also.
As per my post title, GetGadgetAttribute returns an ImageID (handle) which does not work with PB's Image commands (which can be considered a bug or an oversight).
Both mestnyi and Josh get what I was trying to say and Josh even posted a workaround... and yes, this may be better suited for the Feature Request section.


For anyone else following this thread, I'll try my hardest to clear up the confusing mess I've made here due to my inability to convert thoughts into coherent text ;)

Due to PB's flexibility you can have 3 different ways to represent the Image...
hImage=CreateImage(#IMG,w,h) <-- hImage is the ImageID OR #IMG is the Image#
hImage=CreateImage(#PB_Any,w,h) <-- hImage is the Image#
(and the inconsistency ... hImage is an ImageID UNLESS #PB_Any is used then hImage is Image#)

Gadgets that use images all use the ImageID...
ButtonImageGadget(Gadget,x,y,w,h,hImage) <-- if hImage already contains the ImageID
ButtonImageGadget(Gadget,x,y,w,h,ImageID(#IMG)) <--if you are using the Image# it must be converted to ImageID with ImageID()
ButtonImageGadget(Gadget,x,y,w,h,ImageID(hImage)) <--if #PB_Any was used then hImage which is actually Image#, must be converted to ImageID with ImageID()

API calls use hImage (handle) which we either have from PB's ability to provide a handle upfront OR we are provided with ImageID() to use with the Image# to get the handle.
PB Image Commands only use the Image#... ImageWidth(#IMG)
(and the inconsistency... There is no native command to get the Image# from the hImage/handle)

YES, we need GetImageFromHandle(hImage)
This can also be applied to other PB commnds.. GetGadgetFromHandle(hGadget), GetWindowFromHandle(hWindow), etc.

When a command like ImageWidth() can only make use of the Image# even though an Image might have Image# OR ImageID and there is only a command for one way conversion (ImageID()--> hImage) ... this seems to be a bit of a problem. If there was a GetImageFromHandle(hImage) command, this entire post would have been avoided.
Image Image
Post Reply