Is it possible in Gambas to change the text in a label or even text box and then press a control button to create a JPEG of the control itself and save it to disk.
For example if I set the label to Say 'PLS 123.45" with a red background, then simple save the contents of the label as a jpeg.
I could do this in visual basic using a picture box but I seem unable to figure out picture so far on gambas.
Label Control to JPEG
-
- Legend
- Posts: 2081
- Joined: Thu Jul 23, 2020 5:20 pm
- Location: Isle of Wight
Re: Label Control to JPEG
You could just make a picture of the text.
'' Convert label into a picture,
'' use TextSize boolean to make the picture be the size of the text or the picture is a copy of the label as seen
Public Sub Label2Picture(hLabel As Label, Optional TextSize As Boolean) As Picture
Dim hPic As Picture
' size
If TextSize Then ' make the picture the size of the text
hPic = New Picture(hLabel.Font.TextWidth(hLabel.Text), hLabel.Font.TextHeight(hLabel.Text), True)
Else ' make the picture the size of the label
hPic = New Picture(hLabel.W, hLabel.H, True)
Endif
Paint.Begin(hPic)
' background color
Paint.FillRect(0, 0, hPic.W, hPic.H, If(hLabel.Background = -1, Color.Background, hLabel.Background))
' text color
Paint.Font = hLabel.Font
Paint.Brush = Paint.Color(If(hLabel.Foreground = -1, Color.Foreground, hLabel.Foreground))
' text
Paint.DrawText(hLabel.Text, 0, 0, hPic.W, hPic.H, hLabel.Alignment)
Paint.End
Return hPic
End
Re: Label Control to JPEG
To BruceSteer's suggestion I would also add this:
https://www.gambas-it.org/wiki/index.ph ... ontenitore
https://www.gambas-it.org/wiki/index.ph ... ontenitore
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
Re: Label Control to JPEG
This can be done with a PictureBox in Gambas, see if this code helps you. You will need a form with TextBox1, Button1 and PictureBox1.For example if I set the label to Say 'PLS 123.45" with a red background, then simple save the contents of the label as a jpeg.
I could do this in visual basic using a picture box but I seem unable to figure out picture so far on gambas.
Public Sub Button1_Click()
PictureBox1.Picture = Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H)
PictureBox1.Picture.Save(User.Home &/ "Pict.jpg")
End
-
- probation
- Posts: 2
- Joined: Mon Nov 18, 2024 5:43 pm
Re: Label Control to JPEG
Thats it perfect I thought it would be fairly simple thats a nice elegant solution works perfectly. I am getting users to enter data do some calculations and alterations to the text these then get saved as a picture and overlaid on top of an web page that picks up the data and presents it to other users. I couldn't find a reference to the desktop screen shot.cogier wrote: ↑Mon Mar 24, 2025 2:55 pmThis can be done with a PictureBox in Gambas, see if this code helps you. You will need a form with TextBox1, Button1 and PictureBox1.For example if I set the label to Say 'PLS 123.45" with a red background, then simple save the contents of the label as a jpeg.
I could do this in visual basic using a picture box but I seem unable to figure out picture so far on gambas.
Public Sub Button1_Click() PictureBox1.Picture = Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H) PictureBox1.Picture.Save(User.Home &/ "Pict.jpg") End
My visual basic skills used to be very good but everyone seems to be using python to do these things but Im to old to be learning new stuff and when they moved everything to linux I was completely lost. Gambas saves the day . Thank you so much
-
- Legend
- Posts: 2081
- Joined: Thu Jul 23, 2020 5:20 pm
- Location: Isle of Wight
Re: Label Control to JPEG
I did not mention any ScreenShot methods as they are X11 methods only and will not work on wayland.
So just a note:
If you use a wayland system you will find you cannot Screenshot areas of the screen like we can in x11.
wayland will only ever pop open the desktops screenshot dialog (like pressing PrtScr button)
If you need to be compatible with wayland systems too because of that limitation then you can use the (only slightly) more complicated picture method
PS. you could just directly use the Picture object that ScreenShot returns...
So just a note:
If you use a wayland system you will find you cannot Screenshot areas of the screen like we can in x11.
wayland will only ever pop open the desktops screenshot dialog (like pressing PrtScr button)
If you need to be compatible with wayland systems too because of that limitation then you can use the (only slightly) more complicated picture method

PS. you could just directly use the Picture object that ScreenShot returns...
Public Sub Button1_Click()
Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H).Save(User.Home &/ "Pict.jpg")
End