Recent

Author Topic: Text to images Generator (Console app generating bitmaps with text)  (Read 34569 times)

barracuda

  • Full Member
  • ***
  • Posts: 133
Hello,
I am using mint 20 and my question is about creating series of text for test purposes. I have given the question to chatGPT but he simply has not senseful answer. The problem is like that: I need to create text like "A word". But the image must be the dimentions of the word image plus some 2 or 4px around it. I originally tried to write to program in AHK for Windows XP (old version) and found no support for this old app. Maybe I could try Freepascal. I think even the chatGPT could do that but it has no idea how to solve the problem with measuring of the text of the graphic context which does not exist. And it can not to create the graphic context because he does not know the width and height of the image...
So how to solve this "loop" issue?

As I said, I have tried to write the code in AHK so I will try to explain it on the piece of code of it.

I have fonts and texts I want to create.
Code: Pascal  [Select][+][-]
  1. fonts := ["Verdana", "Roboto", "Arial", "Helvetica", "sans-serif"]
  2. texts := ["Psalm 91:1", "Slovo", "AHK pro", "Vygenerování zkušebních obrázků.", "Vygenerování zkušebních obrázků.", "Včera ráno, dnes odpoledne a v neděli večer"]
  3. fontSize := 22.4
  4.  
I create dir..
Code: Pascal  [Select][+][-]
  1. If !FileExist("TestImages_22.4px")
  2.     FileCreateDir, TestImages_22.4px
  3.  
Code: Pascal  [Select][+][-]
  1. Then there is loop for every font.
  2. for font in fonts {
  3. font := Gdip_FontCreate(font, fontSize)
  4.  
and neste loop for every text
Code: Pascal  [Select][+][-]
  1. for text in texts {
Now ChatGPT suggested some shitty code to create image of size 1x1 px - seems nonsense for me

Code: Pascal  [Select][+][-]
  1. image := Gdip_CreateBitmap(1, 1) ; Vytvoření prázdného obrázku pro MeasureString
  2. G := Gdip_GraphicsFromImage(image)
  3.  
G is 0

Or this buggy shi..
Code: Pascal  [Select][+][-]
  1. M := Gdip_MeasureString(0, text, font, 0, &RectF)        
  2. textWidth := M.width
  3.  
Where it used pointer 0 for Graphic Context.
Code: Pascal  [Select][+][-]
  1. and then the map should be created
Code: Pascal  [Select][+][-]
  1. image := Gdip_CreateBitmap(M.Width, M.Height)
And the rest of code inside the loop is
Code: Pascal  [Select][+][-]
  1.         fileName := font . " " . StrSplit(text, " ")[1] . ".png"
  2.         pBrush := Gdip_BrushCreateSolid(0x55FFFFFF) ; Černé pozadí
  3.         Gdip_FillRectangle(G, pBrush, 0, 0, textWidth, y + fontSize) ; Vykreslení pozadí
  4. pBrush := Gdip_BrushCreateSolid(0xFF000000) ; Černé pozadí
  5. Gdip_DrawString(G, text, font, textBrush, x, y)
  6. Gdip_SaveBitmapToFile(image, fileName)
  7. Gdip_DisposeImage(image)
  8. Gdip_DeleteBrush(pBrush)
  9. Gdip_DeleteStringFormat(hFormat)
  10. Gdip_DeleteFont(hFont)
  11. Gdip_DeleteFontFamily(hFamily)
  12.  

I think this should not be hard to do in any programming language and I can create it on linux and copy the files to Windows then.

But what is your idea of solving the problem with measuring of non existing image text?
« Last Edit: November 14, 2023, 01:29:03 pm by barracuda »

wp

  • Hero Member
  • *****
  • Posts: 12595
Re: To Create images with text with GDI+ or what is used by linux
« Reply #1 on: October 28, 2023, 11:16:15 am »
Don't waste your time with ChatGPT...

Measuring text size is one of the standard methods of the LCL canvas: Canvas.TextSize(text), the result is a TSize record with the elements CX (width) and CY (height).

The following code snippet creates a bitmap, sets the font, measures the text, adjusts the bitmap size to the text size + some margin, draws the text and saves the bitmap to file.
Code: Pascal  [Select][+][-]
  1. uses
  2.   Types, LCLType, LCLIntf;
  3.  
  4. { TForm1 }
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. const
  8.   MARGIN = 3;
  9.   TXT = 'A word';
  10. var
  11.   bmp: TBitmap;
  12.   ext: TSize;
  13.   R: TRect;
  14. begin
  15.   bmp := TBitmap.Create;
  16.   try
  17.     // Set the font and measure the text size
  18.     bmp.Canvas.Font.Name := 'Arial';
  19.     bmp.Canvas.Font.Size := 24;
  20.     bmp.Canvas.Font.Color := clRed;
  21.     bmp.Canvas.Font.Style := [fsItalic];
  22.     R.TopLeft := Point(0, 0);
  23.     R.BottomRight := TPoint(bmp.Canvas.TextExtent(TXT));
  24.     // Set the size of the bitmap (= text size + margin)
  25.     bmp.SetSize(R.Right + 2*MARGIN, R.Bottom + 2*MARGIN);
  26.     // Paint the bitmap's background color
  27.     bmp.Canvas.Brush.Color := clWhite;
  28.     bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height);
  29.     // Draw the text
  30.     bmp.Canvas.TextOut(MARGIN, MARGIN, TXT);
  31.     // Save bitmap to file
  32.     bmp.SaveToFile('A_Word.bmp');
  33.   finally
  34.     bmp.Free;
  35.   end;
  36. end;

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #2 on: October 28, 2023, 11:27:45 am »
Thank you but I don't remember how to create  new program. Should I create new Unit?

Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses ...
  8.  

dseligo

  • Hero Member
  • *****
  • Posts: 1460
Re: To Create images with text with GDI+ or what is used by linux
« Reply #3 on: October 28, 2023, 11:39:36 am »
Thank you but I don't remember how to create  new program. Should I create new Unit?

?

https://wiki.freepascal.org/Lazarus_Tutorial#Your_first_Lazarus_Program.21

wp

  • Hero Member
  • *****
  • Posts: 12595
Re: To Create images with text with GDI+ or what is used by linux
« Reply #4 on: October 28, 2023, 12:04:20 pm »
Thank you but I don't remember how to create  new program. Should I create new Unit?
In Lazarus:
  • "File" > "New" > "Project" > "Application" > "OK"
  • In the component palette click on the icon of TButton (the third one on the "Standard" palette). Then click on the form at the point where you want the button to be.
  • Double-click on the button on the form. The code editor appears with an empty ButtonClick procedure. From my previous post, copy the code between ("procedure TForm1.ButtonClick(...)") and "end;" and paste it over the existing empty procedure in your code editor. Add the units Types, LCLType, LCLIntf to the uses clause of the form.
  • Press F9 to compile and run

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #5 on: October 28, 2023, 04:52:36 pm »
Thank you I got error near declaration of bmp and first use of type TRect .

unit2.pas(40,12) Fatal: Syntax error, ";" expected but "identifier BMP" found

I don't see whats wrong

Lazarus 2.2.6


Code: Pascal  [Select][+][-]
  1. unit unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls,
  9.   Graphics, Dialogs, StdCtrls,
  10.   Types, LCLType, LCLIntf;
  11.  
  12. type
  13.  
  14.   { TForm2 }
  15.  
  16.   TForm2 = class(TForm)
  17.     Button1: TButton;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form2: TForm2;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm2 }
  33.  
  34. procedure TForm2.Button1Click(Sender: TObject);
  35.   const MARGIN = 3;
  36.   const TXT = 'A word';
  37. var
  38.   bmp: TBitmap;
  39.   ext: TSize;
  40.   R: TRect;
  41. begin
  42.   bmp := TBitmap.Create;
  43.   try
  44.     // Set the font and measure the text size
  45.     bmp.Canvas.Font.Name := 'Arial';
  46.     bmp.Canvas.Font.Size := 24;
  47.     bmp.Canvas.Font.Color := clRed;
  48.     bmp.Canvas.Font.Style := [fsItalic];
  49.     R.TopLeft := Point(0, 0);
  50.     R.BottomRight := TPoint(bmp.Canvas.TextExtent(TXT));
  51.     // Set the size of the bitmap (= text size + margin)
  52.     bmp.SetSize(R.Right + 2*MARGIN, R.Bottom + 2*MARGIN);
  53.     // Paint the bitmap's background color
  54.     bmp.Canvas.Brush.Color := clWhite;
  55.     bmp.Canvas.FillRect(0, 0, bmp.Width, bmp.Height);
  56.     // Draw the text
  57.     bmp.Canvas.TextOut(MARGIN, MARGIN, TXT);
  58.     // Save bitmap to file
  59.     bmp.SaveToFile('A_Word.bmp');
  60.   finally
  61.     bmp.Free;
  62.   end;
  63. end;
  64.  
  65. end.
  66.  
« Last Edit: October 28, 2023, 05:14:37 pm by barracuda »

wp

  • Hero Member
  • *****
  • Posts: 12595
Re: To Create images with text with GDI+ or what is used by linux
« Reply #6 on: October 28, 2023, 06:49:03 pm »
I don't know - the code is compiling for me... If it still is not working pack the .pas, .lfm, .lpi and .lpr files into a common zip and upload that here ("Attachments and other options") so that I can test the entire project.

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #7 on: October 28, 2023, 07:01:20 pm »
I'll try the code when I will be on Windows, maybe it will compile from there. Thank you for afford.

wp

  • Hero Member
  • *****
  • Posts: 12595
Re: To Create images with text with GDI+ or what is used by linux
« Reply #8 on: October 28, 2023, 08:07:41 pm »
The code is not specific for Windows, it should compile under other operating systems, too.

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #9 on: October 29, 2023, 12:27:36 pm »
Thank you. I have tested it on Windows and no problem. The only problem what I had because I am a newbie using Lazarus, that I have added the unit to already existing project because the old project was opened and I did not close it. It seem that next time I must close the project from Project menu and then select New Project from the same menu...

So I will start linux now because I need to download the fonts which I cannot download on the Windows XP and I can try again to compile it on Linux Mint - Lazarus... and I will ask chatGPT to do the rest of job.

Handoko

  • Hero Member
  • *****
  • Posts: 5387
  • My goal: build my own game engine using Lazarus
Re: To Create images with text with GDI+ or what is used by linux
« Reply #10 on: October 29, 2023, 12:46:42 pm »
If you haven't known, ChatGPT is stupid in Pascal programming.
Better ask me or anyone in the forum.

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #11 on: October 29, 2023, 03:56:53 pm »
I know he is stupid, often it produces irritating results but sometimes a bit more time of tests can bring new questions and new findings... I know my way of learning is strange, but sometimes the code if just find for very basic jobs. If I would bother you will all question, it could be waste of your time...

Handoko

  • Hero Member
  • *****
  • Posts: 5387
  • My goal: build my own game engine using Lazarus
Re: To Create images with text with GDI+ or what is used by linux
« Reply #12 on: October 29, 2023, 04:57:06 pm »
Not wasting my time.

I started learning programming at the beginning of the 90s. There was no internet and programming books were rare to be found. Computing was not taught in schools, I couldn't talk it with my teachers and friends. Computers were very expensive, even some of my friends had it they're not interested in programming. I regularly visited my local bookshops so I won't miss any good books, but almost all of them were for C/C++ and BASIC users.

Programming is hard, there are too many topics to study. I am still learning it. Programming is not my job but I always try to spend some time for it.

I appreciate people who has the passion for learning.
And here I am, to help anyone who want to learn Lazarus.  :)

It's not rare, some users even privately contact me via my email even whatsapp me.

Don't hesitate ask, if you have problem in using Lazarus or Pascal.
Because that's what this forum made for.
« Last Edit: October 29, 2023, 04:59:53 pm by Handoko »

barracuda

  • Full Member
  • ***
  • Posts: 133
Re: To Create images with text with GDI+ or what is used by linux
« Reply #13 on: October 29, 2023, 11:33:47 pm »
Not wasting my time.

I started learning programming at the beginning of the 90s. There was no internet and programming books were rare to be found. Computing was not taught in schools, I couldn't talk it with my teachers and friends. Computers were very expensive, even some of my friends had it they're not interested in programming. I regularly visited my local bookshops so I won't miss any good books, but almost all of them were for C/C++ and BASIC users.

Programming is hard, there are too many topics to study. I am still learning it. Programming is not my job but I always try to spend some time for it.

I appreciate people who has the passion for learning.
And here I am, to help anyone who want to learn Lazarus.  :)

It's not rare, some users even privately contact me via my email even whatsapp me.

Don't hesitate ask, if you have problem in using Lazarus or Pascal.
Because that's what this forum made for.

OK I tried my nerves with chatGPT. This is what he produced. Will it save some time for you? I tried to make him to create the arrays and loops and it should also create the folders, which instruction he probably skipped.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, Forms, Graphics,
  8.   LCLType, LCLIntf;
  9.  
  10. type
  11.   { TForm1 }
  12.   TForm1 = class(TForm)
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.     { Private declarations }
  16.     procedure GenerateImages(fonts: array of string; texts: array of string; fontSize: Integer; folderName: string);
  17.   public
  18.     { Public declarations }
  19.   end;
  20.  
  21. var
  22.   Form1: TForm1;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. { TForm1 }
  29.  
  30. procedure TForm1.GenerateImages(fonts: array of string; texts: array of string; fontSize: Integer; folderName: string);
  31. var
  32.   bmp: TBitmap;
  33.   rect: TRect;
  34.   fileName: string;
  35.   i, j: Integer;
  36. begin
  37.   bmp := TBitmap.Create;
  38.   try
  39.     for i := 0 to High(fonts) do
  40.     begin
  41.       for j := 0 to High(texts) do
  42.       begin
  43.         bmp.Width := 500;
  44.         bmp.Height := 100;
  45.         bmp.Canvas.Font.Name := fonts[i];
  46.         bmp.Canvas.Font.Size := fontSize;
  47.  
  48.         fileName := IncludeTrailingPathDelimiter(folderName) + fonts[i] + ' ' + Copy(texts[j], 1, Pos(' ', texts[j]) - 1) + '.bmp';
  49.  
  50.         bmp.Canvas.Brush.Color := clWhite;
  51.         bmp.Canvas.FillRect(bmp.Canvas.ClipRect);
  52.         bmp.Canvas.TextOut(0, 0, texts[j]);
  53.         bmp.SaveToFile(fileName);
  54.       end;
  55.     end;
  56.   finally
  57.     bmp.Free;
  58.   end;
  59. end;
  60.  
  61. procedure TForm1.FormCreate(Sender: TObject);
  62. begin
  63.   // First batch
  64.   var fonts1 := ['Verdana', 'Roboto', 'Arial', 'Helvetica', 'sans-serif'];
  65.   var texts1 := ['Psalm 91:1', 'Slovo', 'AHK pro', 'Vygenerování zkušebních obrázků.', 'Vygenerování zkušebních obrázků.', 'Včera ráno, dnes odpoledne a v neděli večer'];
  66.   var fontSize1 := 22;
  67.   var folderName1 := 'TestImages_22.4px';
  68.  
  69.   GenerateImages(fonts1, texts1, fontSize1, folderName1);
  70.   ShowMessage('První dávka zkušebních obrázků (22.4px) byla uložena do složky "' + folderName1 + '".');
  71.  
  72.   // Second batch
  73.   var fonts2 := ['Verdana', 'Roboto', 'Arial', 'Helvetica', 'sans-serif'];
  74.   var texts2 := ['Text analysis', 'Slovo', 'AHK pro', 'Vygenerování zkušebních obrázků.', 'Vygenerování zkušebních obrázků.', 'Včera ráno, dnes odpoledne a v neděli večer'];
  75.   var fontSize2 := 16;
  76.   var folderName2 := 'TestImages_16px';
  77.  
  78.   GenerateImages(fonts2, texts2, fontSize2, folderName2);
  79.   ShowMessage('Druhá dávka zkušebních obrázků (16px) byla uložena do složky "' + folderName2 + '".');
  80.  
  81.   // Third batch
  82.   var fonts3 := ['Arial', 'Helvetica', 'Sans-serif'];
  83.   var texts3 := ['Strong''s', 'Hebrew', 'English', 'Morphology', 'Slovo', 'AHK pro', 'Vygenerování zkušebních obrázků.', 'Vygenerování zkušebních obrázků.', 'Včera ráno, dnes odpoledne a v neděli večer'];
  84.   var fontSize3 := 12;
  85.   var folderName3 := 'TestImages_12px';
  86.  
  87.   GenerateImages(fonts3, texts3, fontSize3, folderName3);
  88.   ShowMessage('Třetí dávka zkušebních obrázků (12px) byla uložena do složky "' + folderName3 + '".');
  89.  
  90.   // Fourth batch
  91.   var fonts4 := ['Arial', 'Helvetica', 'Sans-serif'];
  92.   var texts4 := ['Go to Parallel Hebrew', 'Slovo', 'AHK pro', 'Vygenerování zkušebních obrázků.', 'Vygenerování zkušebních obrázků.', 'Včera ráno, dnes odpoledne a v neděli večer'];
  93.   var fontSize4 := 16;
  94.   var folderName4 := 'TestImages_16px';
  95.  
  96.   GenerateImages(fonts4, texts4, fontSize4, folderName4);
  97.   ShowMessage('Čtvrtá dávka zkušebních obrázků (16px) byla uložena do složky "' + folderName4 + '".');
  98. end;
  99.  
  100. end.
  101.  

Frustrated enough tonight.

This is the plan I have created for chatGPT, where the main idea is presented...

Code: Pascal  [Select][+][-]
  1. fonts := ["Verdana", "Roboto", "Arial", "Helvetica", "sans-serif"]
  2. texts := ["Psalm 91:1", "Slovo", "AHK pro", "Vygenerování zkušebních obrázků.", "Vygenerování zkušebních obrázků.", "Včera ráno, dnes odpoledne a v neděli večer"]
  3. fontSize := 22.4
  4.  
  5. If !FileExist("TestImages_22.4px")
  6.     FileCreateDir, TestImages_22.4px
  7.  
  8. for font in fonts
  9.     for text in texts
  10.        
  11. MsgBox, 1st batch (22.4px) saved "TestImages_22.4px".
  12.  
  13. ; Druhá dávka
  14. fonts := ["Verdana", "Roboto", "Arial", "Helvetica", "sans-serif"]
  15. texts := ["Text analysis", "Slovo", "AHK pro", "Vygenerování zkušebních obrázků.", "Vygenerování zkušebních obrázků.", "Včera ráno, dnes odpoledne a v neděli večer"]
  16. fontSize := 16
  17. If !FileExist("TestImages_16px")
  18.     FileCreateDir, TestImages_16px
  19. for font in fonts
  20.     for text in texts
  21. MsgBox, 2nd batch (16px) saved to folder "TestImages_16px".
  22.  
  23. ; Třetí dávka - Velikost 12px pro Arial, Helvetica, Sans-serif:
  24. fonts := ["Arial", "Helvetica", "Sans-serif"]
  25. texts := ["Strong's", "Hebrew", "English", "Morphology", "Slovo", "AHK pro", "Vygenerování zkušebních obrázků.", "Vygenerování zkušebních obrázků.", "Včera ráno, dnes odpoledne a v neděli večer"]
  26. fontSize := 12
  27. If !FileExist("TestImages_12px")
  28.    FileCreateDir, TestImages_12px
  29.  
  30. for font in fonts
  31.    for text in texts
  32. MsgBox, 3td batch  (12px) saved to folder "TestImages_12px".
  33.  
  34. ; Čtvrtá dávka - Velikost 16px pro Arial, Helvetica, Sans-serif:
  35. fonts := ["Arial", "Helvetica", "Sans-serif"]
  36. texts := ["Go to Parallel Hebrew", "Slovo", "AHK pro", "Vygenerování zkušebních obrázků.", "Vygenerování zkušebních obrázků.", "Včera ráno, dnes odpoledne a v neděli večer"]
  37. fontSize := 16
  38. If !FileExist("TestImages_16px")
  39.    FileCreateDir, TestImages_16px
  40. for font in fonts  
  41.    for text in texts {
  42. MsgBox, 4th batch (16px) saved
  43.  

My brain is not so flexible to learn, getting older.

So any help appreciated. Tomorrow I will be more relaxed to spend more time with it.
« Last Edit: October 29, 2023, 11:35:44 pm by barracuda »

Handoko

  • Hero Member
  • *****
  • Posts: 5387
  • My goal: build my own game engine using Lazarus
Re: To Create images with text with GDI+ or what is used by linux
« Reply #14 on: October 30, 2023, 07:43:07 am »
wp's code should work. But if you have problem using it, you can download this fully working code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Graphics, StdCtrls, Types;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.     function  FontIsAvailable(const FontName: string): Boolean;
  21.     procedure Generate(const FontName: string; FontSize: Integer;
  22.       MarginX, MarginY: Integer; const Message, FileName: string);
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. const
  31.   Names:    array[0..2] of string = ('img1.bmp', 'img2.bmp', 'img3.bmp');
  32.   Fonts:    array[0..2] of string = ('Dejavu Sans', 'Arial', 'FreeMono');
  33.   Messages: array[0..2] of string = ('Lazarus is fun.',
  34.                                      'Let''s learn programming!',
  35.                                      'Hooray');
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.Button1Click(Sender: TObject);
  42. var
  43.   FontName: string;
  44.   i: Integer;
  45. begin
  46.   for i := Low(Names) to High(Names) do
  47.   begin
  48.     FontName := Fonts[i];
  49.     if not(FontIsAvailable(FontName)) then
  50.       Memo1.Lines.Add(Fonts[i] + ' is not available.');
  51.     Generate(FontName, 12, 8, 4, Messages[i], Names[i]);
  52.   end;
  53.   Memo1.Lines.Add(IntToStr(Length(Names)) + ' images generated.');
  54. end;
  55.  
  56. procedure TForm1.FormCreate(Sender: TObject);
  57. begin
  58.   Memo1.Clear;
  59. end;
  60.  
  61. function TForm1.FontIsAvailable(const FontName: string): Boolean;
  62. begin
  63.   Result := Screen.Fonts.IndexOf(FontName) >= 0;
  64. end;
  65.  
  66. procedure TForm1.Generate(const FontName: string; FontSize: Integer; MarginX,
  67.   MarginY: Integer; const Message, FileName: string);
  68. var
  69.   Output: TBitmap;
  70.   Size:   TSize;
  71.   SizeX:  Integer;
  72.   SizeY:  Integer;
  73. begin
  74.   Output := TBitmap.Create;
  75.   Output.SetSize(100, 100);
  76.   Output.Canvas.Brush.Style := bsClear;
  77.   Output.Canvas.Font.Name := FontName;
  78.   Output.Canvas.Font.Size := FontSize;
  79.   Size  := Output.Canvas.TextExtent(Message);
  80.   SizeX := MarginX + Size.Width + MarginX;
  81.   SizeY := MarginY + Size.Height + MarginY;
  82.   Output.SetSize(SizeX, SizeY);
  83.   Output.Canvas.Brush.Color := clWhite;
  84.   Output.Canvas.FillRect(0, 0, SizeX, SizeY);
  85.   Output.Canvas.TextOut(MarginX, MarginY, Message);
  86.   Output.SaveToFile(FileName);
  87.   Output.Free;
  88. end;
  89.  
  90. end.

  • You can copy/paste the Generate procedure for used in other projects, see line #66.
  • Not sure on other platforms but on Ubuntu Mate, wp's code need to have brush style to be set before calling TextExtent, see line #76.
  • Linux does not have same default fonts installed as in Windows. So, you can use FontIsAvailable function to check the availability, see line #61.
  • You can change the image's names, fonts and texts in line #31 .. #33.

 

TinyPortal © 2005-2018