[Sloved] Help with getting a image to fit onto a button

Post your Gambas programming questions here.
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

[Sloved] Help with getting a image to fit onto a button

Post by AndyGable »

Hi Everyone

I am using the following code to load a image from the database onto a key

Public Function ImageFromString(ImageString As String, ImageFile As Button)
    If ImageString <> "" Then 
        Global.pc = Picture.FromString(FromBase64(ImageString))  
        ImageFile.Picture = Global.pc  
    End If
End


Does anyone know how I can make the image fit onto the button and leave a space at the bottom for the text of the button?

I have a Windows application that loads the image and on that it fits fine but when I load the data in on the Gambas app the image is not fitting on the key

Any pointers is welcome :)
Last edited by AndyGable on Sunday 28th January 2024 11:51pm, edited 1 time in total.
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

use the Picture.Stretch() method
https://gambaswiki.org/wiki/comp/gb.qt4/picture/stretch


Public Function ImageFromString(ImageString As String, ImageFile As Button)
    If ImageString <> "" Then 
        Global.pc = Picture.FromString(FromBase64(ImageString))  
        ImageFile.Picture = Global.pc.Stretch(ImageFile.Height-4)  ' shrink image to less than button height with 2px margin
    End If
End


and with gambas button images are placed to the left of the text not above or below
you'll have to make a custom button/function to do that
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

BruceSteers wrote: Saturday 27th January 2024 12:43am
and with gambas button images are placed to the left of the text not above or below
you'll have to make a custom button/function to do that
Here is a function that does that trick.
It makes a picture that consists of the icon and Paints the text below the icon. then sets the button picture as the picture with text.

Note: do not set Button1.Text as it is now in the image

EDIT: fixed


Public Sub Form_Open()
  
  ' make Button1.Picture a picture with text below it.
  MakePictureText(Button1, Picture["icon:/32/alarm"], "Alarm call")

End

'' Make a picture with text below it. can be used on any object that has a .Text and a .Picture property.
Public Sub MakePictureText(Obj As Object, Pic As Picture, Text As String)
  
  Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.

  Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  Obj.Text = ""  ' make sure Obj has not got text

  ' use Paint.class to draw the icon and text to the picture
  Paint.Begin(p)
  Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  Paint.End

  Obj.Picture = p  ' set the object picture to new pic

End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Help with getting a image to fit onto a button

Post by cogier »

Here is a function that does that trick.
It does, BUT only with gb.gui. If I use gb.gui.qt all I get is a very black button!
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

cogier wrote: Saturday 27th January 2024 5:46pm
Here is a function that does that trick.
It does, BUT only with gb.gui. If I use gb.gui.qt all I get is a very black button!
That must be a bug in gb.qt5 then. the code is sound.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

i've reported the bug.

here's how it looks on my system...
Untitled.jpg
Untitled.jpg (14.3 KiB) Viewed 2093 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

Seems to work okay on qt5 if i give the 3rd Transparent argument with New Picture

(I fixed the example above)

 

  Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new "Transparent" picture

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

i found this also works on qt..


  Dim p As Picture = New Picture(Obj.W, Obj.H) ' make a new picture.
  p.Fill(Color.ButtonBackground) ' fill it with the ButtonBackground color first



So I guess qt just does not initialize a picture cleanly unless explicitly told to be transparent.
And if not transparent then a bg color needs painting first.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Help with getting a image to fit onto a button

Post by BruceSteers »

I got this reply on the bugtracker...
Benoit wrote: Like Image, Picture contents is uninitialized by default - but if it's not specified in the wiki.

GTK+ component is a bit different, because Image and Picture are actually the same object internally.

So, your new Image or Picture may be initialized by default, but you can't rely on that, and suppose that
the initial contents is random.
I have updated the wiki Picture._new page to state this.
https://gambaswiki.org/edit/comp/gb.qt4/picture/_new

So i guess my "workarounds" are not so much workarounds and more like "how you should properly do it" :-/

lol
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Help with getting a image to fit onto a button

Post by AndyGable »

Thank you to everyone who replied to this problem the code that BruceSteers supplied works beautifully and the images look great

Thank you BruceSteers you have gotten me out of some tight issues in the past with this project.
Post Reply