TImageBase64
Source: source/classes/imageb64.prg
Standalone utility class
TImageBase64 is a utility class that encodes images to Base64 for use in web pages (HTML <img> tags with data URIs). It can also extract and save images from Base64-encoded HTML. The class auto-detects whether the input source is a file path, URL, or plain text.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
aImage | Array | Internal image array from FW_ReadImage |
cFile | Character | Source file path or URL (if applicable) |
cBase64 | Character | Base64 encoded image string |
lTag | Logical | Include <img> tags in output |
lHtml | Logical | Generate full HTML document in output |
lUrlEncode | Logical | URL-encode the data URI instead of plain base64 |
nImgWidth | Numeric | Image width in pixels (ACCESS readonly) |
nImgHeight | Numeric | Image height in pixels (ACCESS readonly) |
Methods
| Method | Description |
|---|---|
New( cSource ) | Create object; optionally load source immediately |
SetSource( cSource ) | Set source: auto-detects file, URL, or base64 text |
ChooseFile() | Open file picker dialog to select an image |
Paste() | Load image from clipboard |
MakeText( lUrlEncode, lTag, lHtml, cTitle ) | Generate output text (data URI or HTML) |
SaveText( cFile, lUrlEncode, lTag, lHtml ) | Save output text to file; returns filename or nil |
CopyText( lUrlEncode, lTag, lHtml ) | Copy output text to clipboard; returns the text |
SaveImage() | Save the decoded image to a file (.jpg or .png) |
TestHTML() | Generate a test HTML file and open it in the browser |
Example: Image to Base64 HTML Tag
#include "FiveWin.ch"
function Main()
local oImg := TImageBase64():New( "c:\images\photo.jpg" )
// Generate an HTML img tag with inline base64 data
oImg:lTag := .T.
oImg:CopyText() // copies to clipboard
// Or save as a complete HTML file
oImg:lHtml := .T.
oImg:SaveText( "preview.html" )
// Open in browser to test
oImg:TestHTML()
return nil
Notes
- The source auto-detection checks: file path (<=255 chars, exists on disk), HTTP/HTTPS URL, or base64-encoded text.
- Use
Paste()to grab an image directly from the Windows clipboard (bitmap, file list, or PNG format). - The
nLineWidthclass data (default 1024) controls line breaking in the base64 output. - Supported output formats:
data:image/...URIs,<img>tags, or full HTML documents.