Sample code for 30+ languages & platforms
C

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat C Downloads

C
#include <C_CkAi.h>
#include <C_CkJsonObject.h>
#include <C_CkBinData.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkAi ai;
    HCkJsonObject askParams;
    HCkBinData bdImageData;

    success = FALSE;

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

    ai = CkAi_Create();

    CkAi_putProvider(ai,"openai");

    // Use your provider's API key.
    CkAi_putApiKey(ai,"MY_API_KEY");

    // Choose a model.
    CkAi_putModel(ai,"gpt-5");

    askParams = CkJsonObject_Create();
    CkJsonObject_UpdateString(askParams,"image.output_format","jpeg");
    CkJsonObject_UpdateString(askParams,"image.size","1024x1024");
    CkJsonObject_UpdateString(askParams,"image.quality","low");
    CkAi_SetAskParams(ai,askParams);

    CkAi_InputAddText(ai,"Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf");

    // Ask the AI for image output.
    success = CkAi_Ask(ai,"image");
    if (success == FALSE) {
        printf("%s\n",CkAi_lastErrorText(ai));
        CkAi_Dispose(ai);
        CkJsonObject_Dispose(askParams);
        return;
    }

    // Get the image response data.
    bdImageData = CkBinData_Create();
    success = CkAi_GetOutputBd(ai,bdImageData);
    if (success == FALSE) {
        printf("%s\n",CkAi_lastErrorText(ai));
        CkAi_Dispose(ai);
        CkJsonObject_Dispose(askParams);
        CkBinData_Dispose(bdImageData);
        return;
    }

    CkBinData_WriteFile(bdImageData,"c:/aaworkarea/out.jpg");

    printf("Success.\n");


    CkAi_Dispose(ai);
    CkJsonObject_Dispose(askParams);
    CkBinData_Dispose(bdImageData);

    }