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

Post

Posted
Rating:
#1 (In Topic #1180)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

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

Code (gambas)

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

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 :)
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
use the Picture.Stretch() method
/comp/gb.qt4/picture/stretch - Gambas Documentation

Code (gambas)

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

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
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’

BruceSteers said


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

Code (gambas)

  1.  
  2. Public Sub Form_Open()
  3.  
  4.   ' make Button1.Picture a picture with text below it.
  5.   MakePictureText(Button1, Picture["icon:/32/alarm"], "Alarm call")
  6.  
  7.  
  8. '' Make a picture with text below it. can be used on any object that has a .Text and a .Picture property.
  9. Public Sub MakePictureText(Obj As Object, Pic As Picture, Text As String)
  10.  
  11.   Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.
  12.  
  13.   Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  14.   Obj.Text = ""  ' make sure Obj has not got text
  15.  
  16.   ' use Paint.class to draw the icon and text to the picture
  17.   Paint.Begin(p)
  18.   Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  19.   Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  20.   Paint.End
  21.  
  22.   Obj.Picture = p  ' set the object picture to new pic
  23.  
  24.  
  25.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
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!
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

cogier said

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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
i've reported the bug.

here's how it looks on my system…
Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
Seems to work okay on qt5 if i give the 3rd Transparent argument with New Picture

(I fixed the example above)

Code (gambas)

  1.  
  2.  
  3.   Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new "Transparent" picture
  4.  
  5.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
i found this also works on qt..

Code (gambas)

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

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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
I got this reply on the bugtracker…

Benoit said

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.
/comp/gb.qt4/picture/_new - Gambas Documentation

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

 lol
Online now: No Back to the top

Post

Posted
Rating:
#10
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

Code (gambas)

  1. Public Sub Form_Open()
  2.   ' make Button1.Picture a picture with text below it.
  3.   MakePictureText(Button1, Picture["icon:/32/alarm"], "Alarm call")
  4.  
  5. '' Make a picture with text below it. can be used on any object that has a .Text and a .Picture property.
  6. Public Sub MakePictureText(Obj As Object, Pic As Picture, Text As String)
  7.  
  8.   Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.
  9.  
  10.   Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  11.   Obj.Text = ""  ' make sure Obj has not got text
  12.  
  13.   ' use Paint.class to draw the icon and text to the picture
  14.   Paint.Begin(p)
  15.   Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  16.   Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  17.   Paint.End
  18.  
  19.   Obj.Picture = p  ' set the object picture to new pic
  20.  
  21.  
  22.  

Hi BruceSteers
Quick question about your excellent code that works a treat what do I change to make the text a size smaller?
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’

AndyGable said

BruceSteers said

Code (gambas)

  1. Public Sub Form_Open()
  2.   ' make Button1.Picture a picture with text below it.
  3.   MakePictureText(Button1, Picture["icon:/32/alarm"], "Alarm call")
  4.  
  5. '' Make a picture with text below it. can be used on any object that has a .Text and a .Picture property.
  6. Public Sub MakePictureText(Obj As Object, Pic As Picture, Text As String)
  7.  
  8.   Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.
  9.  
  10.   Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  11.   Obj.Text = ""  ' make sure Obj has not got text
  12.  
  13.   ' use Paint.class to draw the icon and text to the picture
  14.   Paint.Begin(p)
  15.   Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  16.   Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  17.   Paint.End
  18.  
  19.   Obj.Picture = p  ' set the object picture to new pic
  20.  
  21.  
  22.  

Hi BruceSteers
Quick question about your excellent code that works a treat what do I change to make the text a size smaller?

You're welcome
Use Paint.Font to change the text attributes :)

Code (gambas)

  1.   Paint.Begin(p)
  2.   Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  3.   Paint.Font.Size = 8
  4.   Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  5.   Paint.End
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#13
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

BruceSteers said

You're welcome
Use Paint.Font to change the text attributes :)

Code (gambas)

  1.   Paint.Begin(p)
  2.   Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  3.   Paint.Font.Size = 8
  4.   Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  5.   Paint.End
  6.  

So simple I though it was going to be something super complicated  :D thank you
Online now: No Back to the top
1 guest and 0 members have just viewed this.