TTMSMCPCloudImageAI generates and edits images through a cloud service, and presents OpenAI, Google Gemini, Black Forest Labs, Stability and Reve behind one API. You set APIKey, pick a Service, and call Execute with a prompt; the image comes back as base64 in OnImageGenerated whatever the service returned on the wire. The same Execute takes a reference image — as a stream, a base64 string, or a framework picture — to edit an existing image instead of creating one, and the Images collection turns a single prompt into a multi-reference composition. RemoveBackground and ReplaceBackground cover the two edits that are asked for most often, using the service's dedicated endpoint where one exists and a generated instruction where it does not. Model and CustomOptions reach whatever the individual provider supports beyond that.
The request both result events carry: status code, body, headers, and success flag.
Minimal example
Generate an image from a promptPascalC++
procedure TForm1.FormCreate(Sender: TObject);
begin
{ ImageAI is a TTMSMCPCloudImageAI on the form. The key comes from the
application configuration - never from a literal in the source. }
ImageAI.APIKey := Config.ReadString('imageai', 'openai_key', '');
ImageAI.Service := isOpenAI;
ImageAI.OnImageGenerated := ImageAIImageGenerated;
ImageAI.OnRequestError := ImageAIRequestError;
end;
procedure TForm1.btnGenerateClick(Sender: TObject);
begin
{ Execute returns straight away; the result arrives in OnImageGenerated. }
ImageAI.Execute('A photorealistic image of a cat doing yoga.');
end;
procedure TForm1.ImageAIImageGenerated(Sender: TObject;
ARequestResult: TTMSMCPCloudBaseRequestResult; ABase64Image: string);
var
Stream: TMemoryStream;
begin
{ Every service hands the image back as base64, whatever it returned on
the wire, so one decoding path covers all five. }
Stream := TMemoryStream.Create;
try
TTMSMCPUtils.LoadStreamFromBase64(ABase64Image, Stream);
Stream.SaveToFile(TPath.Combine(FOutputFolder, 'generated.png'));
finally
Stream.Free;
end;
end;
procedure TForm1.ImageAIRequestError(Sender: TObject;
ARequestResult: TTMSMCPCloudBaseRequestResult);
begin
memoLog.Lines.Add(Format('Image request failed (%d): %s',
[ARequestResult.ResponseCode, ARequestResult.ResultString]));
end;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// ImageAI is a TTMSMCPCloudImageAI on the form. The key comes from the
// application configuration - never from a literal in the source.
ImageAI->APIKey = Config->ReadString("imageai", "openai_key", "");
ImageAI->Service = isOpenAI;
ImageAI->OnImageGenerated = ImageAIImageGenerated;
ImageAI->OnRequestError = ImageAIRequestError;
}
void __fastcall TForm1::btnGenerateClick(TObject *Sender)
{
// Execute returns straight away; the result arrives in OnImageGenerated.
ImageAI->Execute("A photorealistic image of a cat doing yoga.");
}
void __fastcall TForm1::ImageAIImageGenerated(TObject *Sender,
TTMSMCPCloudBaseRequestResult *ARequestResult, System::UnicodeString ABase64Image)
{
// Every service hands the image back as base64, whatever it returned on
// the wire, so one decoding path covers all five.
TMemoryStream *Stream = new TMemoryStream();
try
{
Tms::Mcp::Utils::TTMSMCPUtils::LoadStreamFromBase64(ABase64Image, Stream);
Stream->SaveToFile(System::Ioutils::TPath::Combine(FOutputFolder, "generated.png"));
}
__finally
{
delete Stream;
}
}
void __fastcall TForm1::ImageAIRequestError(TObject *Sender,
TTMSMCPCloudBaseRequestResult *ARequestResult)
{
memoLog->Lines->Add(Format("Image request failed (%d): %s",
ARRAYOFCONST((ARequestResult->ResponseCode, ARequestResult->ResultString))));
}
Text to image, editing one reference image, several references through the Images collection, background removal and replacement, decoding and saving the result, and handling failures.
Choosing a service and the key that belongs to it, model names and their defaults, service-specific CustomOptions, the polling services that answer in several round trips, and request logging.
See Also
Cloud AI — text, files, tools, speech, and transcription against the same kind of service