Skip to content

Get Base64 encoding for images in TMS Web Core

Not so long ago, I posted about how to convert an image into a Base64 string. Today, I will show you that using TMS Web Core, this is even easier.

If we look into the source code of TMS Web Core, we find that the custom image control adds a public property called Base64Image of type string. That means, we can read the Base64 string of an image using that property. However, this is a read-only property. You cannot assign a Base64-string to it in order to assign image data. I will show a different approach how to work with Base64-strings for images in one of my next posts.

  TCustomImageControl = class(TCustomControl)
    // ...
  public  
    // ...
    property Base64Image: string read GetBase64Img;
  end;

Let’s try this functionality. Drop (1) a TWebImage (WebImage) and a (2) TWebMemo (WebMemo) onto the form.

Screenshot

Implement the OnCreate event of the form as follows to load an image and assign its Base64 representation to the memo:

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  WebImage.URL :=
    'https://images.pexels.com/photos/' +
    '2355519/pexels-photo-2355519.jpeg';
  WebMemo.Lines.Text := WebImage.Base64Image;
end;

Running the application results in the following app in the web browser:

Screenshot

Note that we can also assign a URL to the web image component in order to load an image.

Cover

TMS WEB Core Book, 2nd Edition

If you want to learn more about TMS WEB Core, please have a look at my book that is designed for beginners that want to learn about Web development with Delphi as well as advanced developers that want to get the most out of TMS WEB Core. Examples in the book go in much more detail and as all basics are covered as well, you can always go back to read about it again. Something these tutorials simply cannot provide.

Go to https://flixengineering.com/books to learn more. If you are located in the US, simply order at Amazon 🇺🇸.