Sample code for 30+ languages & platforms
DataFlex

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoAi
    Variant vAskParams
    Handle hoAskParams
    Variant vBdImageData
    Handle hoBdImageData
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatAi)) To hoAi
    If (Not(IsComObjectCreated(hoAi))) Begin
        Send CreateComObject of hoAi
    End

    Set ComProvider Of hoAi To "openai"

    // Use your provider's API key.
    Set ComApiKey Of hoAi To "MY_API_KEY"

    // Choose a model.
    Set ComModel Of hoAi To "gpt-5"

    Get Create (RefClass(cComChilkatJsonObject)) To hoAskParams
    If (Not(IsComObjectCreated(hoAskParams))) Begin
        Send CreateComObject of hoAskParams
    End
    Get ComUpdateString Of hoAskParams "image.output_format" "jpeg" To iSuccess
    Get ComUpdateString Of hoAskParams "image.size" "1024x1024" To iSuccess
    Get ComUpdateString Of hoAskParams "image.quality" "low" To iSuccess
    Get pvComObject of hoAskParams to vAskParams
    Get ComSetAskParams Of hoAi vAskParams To iSuccess

    Get ComInputAddText Of hoAi "Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf" To iSuccess

    // Ask the AI for image output.
    Get ComAsk Of hoAi "image" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the image response data.
    Get Create (RefClass(cComChilkatBinData)) To hoBdImageData
    If (Not(IsComObjectCreated(hoBdImageData))) Begin
        Send CreateComObject of hoBdImageData
    End
    Get pvComObject of hoBdImageData to vBdImageData
    Get ComGetOutputBd Of hoAi vBdImageData To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComWriteFile Of hoBdImageData "c:/aaworkarea/out.jpg" To iSuccess

    Showln "Success."


End_Procedure