Label Control to JPEG

Post your Gambas programming questions here.
Post Reply
pocketpete
probation
Posts: 2
Joined: Mon Nov 18, 2024 5:43 pm

Label Control to JPEG

Post by pocketpete »

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.
BruceSteers
Legend
Posts: 2081
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: Label Control to JPEG

Post by BruceSteers »

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

vuott
Regular
Posts: 310
Joined: Wed Apr 05, 2017 6:07 pm
Location: European Union

Re: Label Control to JPEG

Post by vuott »

To BruceSteer's suggestion I would also add this:

https://www.gambas-it.org/wiki/index.ph ... ontenitore
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
cogier
Site Admin
Posts: 1196
Joined: Wed Sep 21, 2016 2:22 pm
Location: Guernsey, Channel Islands

Re: Label Control to JPEG

Post by cogier »

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.
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.

Public Sub Button1_Click()

  PictureBox1.Picture = Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H)
  PictureBox1.Picture.Save(User.Home &/ "Pict.jpg")

End
  
pocketpete
probation
Posts: 2
Joined: Mon Nov 18, 2024 5:43 pm

Re: Label Control to JPEG

Post by pocketpete »

cogier wrote: Mon Mar 24, 2025 2:55 pm
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.
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.

Public Sub Button1_Click()

  PictureBox1.Picture = Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H)
  PictureBox1.Picture.Save(User.Home &/ "Pict.jpg")

End
  
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.

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
BruceSteers
Legend
Posts: 2081
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: Label Control to JPEG

Post by BruceSteers »

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...
Public Sub Button1_Click()
 
  Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H).Save(User.Home &/ "Pict.jpg")
 
End
Post Reply