0

I use Gambas3 in Ubuntu 20.04. I need write some text in a image and create a new image. JPG o BMP

  • Ok, please add your latest/greatest code to your question by clicking [edit]. Are you using a library? Which? Is the text visible in the image, or embedded as a comment? Do you need varying colours? Fonts? Sizes? – Mark Setchell Aug 09 '22 at 20:25
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 10 '22 at 13:15

2 Answers2

1

I resolve my problem with this code; load a image into drawing area, then draw a text with shadow for best visibility, then save in format JPG, and then load the new image into other drawing area. Not sure if this is the best option, but it works.

  • Gambas 3.17.3
  • Ubuntu 20.04
  • libs gb.gtk and gb.image
  • Image size 800x533 pixels

My code:

' Gambas class file
 
 Public Sub Form_Open()
  DrawingArea1.Background = Color.white
 End
 
Public Sub DrawingArea1_Draw()
  dibu()
 
End
 
Private Sub dibu()
 
  Dim X, Y, W, H As Float
  Dim hBrush As PaintBrush
  Dim hImage As Image
 
  hImage = Image.Load("bird212.jpg")
 
  X = 0
  Y = 0
  W = 800
  H = 533
 
  hBrush = Paint.Image(hImage)
  hBrush.Translate(X, Y)
  Paint.Brush = hBrush
  Paint.Rectangle(X, Y, W, H)
  Paint.Fill
  Paint.Stroke
 
 
  Paint.Font.Name = "Mono"
  Paint.Font.Size = 12
  Paint.Font.Bold = True
  Paint.Brush = Paint.Color(Color.White)      
  Paint.DrawRichTextShadow("Hello WORLD 12.345", 10, 500, 300, 50,,, 1)
  Paint.Fill
  Paint.Stroke

  Paint.Brush = Paint.Color(Color.Black) 
  Paint.DrawRichText("Hello WORLD 12.345", 10, 500, 300, 50)
  Paint.Fill
  Paint.Stroke
   
End
 
Public Sub ButtonSaveImage_Click()
 
  Dim filex As Picture
  
  filex = New Picture(drawingArea1.w, drawingArea1.h, Color.Transparent) 'probar...
  Paint.begin(filex)
  dibu()
  paint.end
 
  filex.save(user.home & "/" & "prub.jpg")
  Label1.text = "Image saved in: " & user.home & "/" & "prub.jpg"
  PictureBox2.Picture = Picture.Load(user.home & "/" & "prub.jpg")
 
End
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

You have over-complicated it I think.

As you can use the Paint.class on Images and pictures directly and as you can load an Image directly into a PictureBox you could simply do the following to load an image into a picturebox with custom text...

  Public Sub btnSetImage_Click()
  
    Dim hImage As Image
    Dim sText As String = "Hello World 12.345"
  
    hImage = Image.Load("bird212.jpg")
    Paint.Begin(hImage)
    Paint.Font = Font["Mono, 12, bold"]
    Paint.Background = Color.White
    Paint.DrawRichTextShadow(sText, 0, hImage.Height - Paint.Font.Height * 2, Me.Width, Paint.Font.Height, Align.Center,, 1)
    Paint.Stroke
    Paint.Background = Color.black
    Paint.DrawRichText(sText, 0, hImage.Height - Paint.Font.Height * 2, Me.Width, Paint.Font.Height, Align.Center)
    Paint.End
  
    Try PictureBox2.Image.Clear
    PictureBox2.Image = hImage
  
  End

Public Sub btnSaveImage_Click()

  PictureBox2.Picture.Save(user.home &/  "prub.jpg")
  Label1.text = "Image saved in: " & user.home &/ "prub.jpg"

End