Label Control to JPEG

Post

Posted
Rating:
#1 (In Topic #1444)
Trainee
pocketpete is in the usergroup ‘unknown’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Banned
You could just make a picture of the text.

Code (gambas)

  1. '' Convert label into a picture,
  2. '' use TextSize boolean to make the picture be the size of the text or the picture is a copy of the label as seen
  3. Public Sub Label2Picture(hLabel As Label, Optional TextSize As Boolean) As Picture
  4.  
  5.   Dim hPic As Picture
  6. ' size
  7.   If TextSize Then  '  make the picture the size of the text
  8.     hPic = New Picture(hLabel.Font.TextWidth(hLabel.Text), hLabel.Font.TextHeight(hLabel.Text), True)
  9.   Else  ' make the picture the size of the label
  10.     hPic = New Picture(hLabel.W, hLabel.H, True)
  11.  
  12.   Paint.Begin(hPic)
  13. ' background color
  14.   Paint.FillRect(0, 0, hPic.W, hPic.H, If(hLabel.Background = -1, Color.Background, hLabel.Background))
  15.  
  16. ' text color
  17.   Paint.Font = hLabel.Font
  18.   Paint.Brush = Paint.Color(If(hLabel.Foreground = -1, Color.Foreground, hLabel.Foreground))
  19. ' text
  20.   Paint.DrawText(hLabel.Text, 0, 0, hPic.W, hPic.H, hLabel.Alignment)
  21.   Paint.End
  22.  
  23.   Return hPic
  24.  
  25.  
  26.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
vuott is in the usergroup ‘Regular’

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
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.

Code (gambas)

  1. Public Sub Button1_Click()
  2.  
  3.   PictureBox1.Picture = Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H)
  4.   PictureBox1.Picture.Save(User.Home &/ "Pict.jpg")
  5.  
  6.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
pocketpete is in the usergroup ‘unknown’

cogier said

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.

Code (gambas)

  1. Public Sub Button1_Click()
  2.  
  3.   PictureBox1.Picture = Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H)
  4.   PictureBox1.Picture.Save(User.Home &/ "Pict.jpg")
  5.  
  6.  

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

Post

Posted
Rating:
#6
Banned
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…

Code (gambas)

  1. Public Sub Button1_Click()
  2.  
  3.   Desktop.Screenshot(TextBox1.ScreenX, TextBox1.ScreenY, TextBox1.W, TextBox1.H).Save(User.Home &/ "Pict.jpg")
  4.  
  5.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.