Recent

Author Topic: Create a png image from a string  (Read 1058 times)

PepperJules

  • Newbie
  • Posts: 5
Create a png image from a string
« on: July 09, 2025, 05:32:46 pm »
I have difficulties to find out where to start, and I hope some of you experienced coders can point
me in the right direction...

I have a variable containing a string, and would like to create a png image from it.
I would like to be able to set the text font, text color and text size, as well as the background color
of the image (black, white or alpha).

The image file format can also be jpg.

What units/components should I be looking at?
Platform = windows

thijsvandien

  • Newbie
  • Posts: 3
Re: Create a png image from a string
« Reply #1 on: July 09, 2025, 06:53:41 pm »
Forget about the final image format for now. As long as you can produce a bitmap (basically a grid of pixels), it can later be converted to something more efficient. JPEG doesn't support transparency, however.

Here's an intro:
https://wiki.freepascal.org/Developing_with_Graphics


jamie

  • Hero Member
  • *****
  • Posts: 7829
Re: Create a png image from a string
« Reply #2 on: July 09, 2025, 11:56:15 pm »
There are some considerations you must examine.

 You do not indicate if this string variable is to be used over the network, so that would mean not using upper 128 chars?

 You have a choice of using a encoding 64 functions as such.

 a human readable string would mean encoding the image into a Hex string, which will make it twice the size but at least if can be contained in a string without issues.


 You need to specify more details!

Jamie

The only true wisdom is knowing you know nothing

TBMan

  • Sr. Member
  • ****
  • Posts: 355
Re: Create a png image from a string
« Reply #3 on: July 10, 2025, 12:55:14 am »
I have difficulties to find out where to start, and I hope some of you experienced coders can point
me in the right direction...

I have a variable containing a string, and would like to create a png image from it.
I would like to be able to set the text font, text color and text size, as well as the background color
of the image (black, white or alpha).

The image file format can also be jpg.

What units/components should I be looking at?
Platform = windows

It sounds like you want to learn how to code run-length encoded image files.
*OR

You want to save current display options in a program which is different.
« Last Edit: July 10, 2025, 01:01:20 am by TBMan »
Barry

Newest game (clone),
Missile Commander:
https://www.youtube.com/watch?v=tgKz0cxog-k

Khrys

  • Sr. Member
  • ****
  • Posts: 467
Re: Create a png image from a string
« Reply #4 on: July 10, 2025, 07:05:09 am »
Here's a very barebones example program that creates an image, draws some text on it, and saves it as a  .png:

Code: Pascal  [Select][+][-]
  1. program Example;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Interfaces, Graphics;
  7.  
  8. procedure DrawExample(const Text: String);
  9. var
  10.   Image: TPicture;
  11. begin
  12.  
  13.   // Create blank 256 x 256 image
  14.   Image := TPicture.Create();
  15.   Image.PNG.SetSize(256, 256);
  16.  
  17.   // Draw something using the canvas
  18.   with Image.PNG.Canvas do begin
  19.  
  20.     // Fill background color
  21.     Brush.Color := TColor($F7F4F0);
  22.     Brush.Style := bsSolid;
  23.     FillRect(0, 0, Image.Width, Image.Height);
  24.  
  25.     // Draw text
  26.     Font.Color := clTeal;
  27.     Font.Name := 'Arial';
  28.     Font.Height := 48;
  29.     TextOut(16, 96, Text);
  30.   end;
  31.  
  32.   // Save & destroy image
  33.   Image.SaveToFile('test.png');
  34.   Image.Free();
  35. end;
  36.  
  37. begin
  38.   DrawExample('Hello World!');
  39. end.

Code: Bash  [Select][+][-]
  1. fpc example.pas ^
  2.     -FuC:\lazarus\lcl\units\x86_64-win64 ^
  3.     -FuC:\lazarus\lcl\units\x86_64-win64\win32 ^
  4.     -FuC:\lazarus\components\lazutils\lib\x86_64-win64

If you want to do anything involving transparency that is more complicated than filling non-antialiased rectangles (i.e. pretty much anything), I'd encourage you to switch to BGRABitmap; it's a household name in the Free Pascal / Lazarus ecosystem and has lots of features, although its documentation is a bit lacking.
« Last Edit: July 10, 2025, 01:00:20 pm by Khrys »

Boleeman

  • Hero Member
  • *****
  • Posts: 1160
Re: Create a png image from a string
« Reply #5 on: July 10, 2025, 07:29:48 am »
And a Text on Paintbox example.

paweld

  • Hero Member
  • *****
  • Posts: 1676
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018