Sample code for 30+ languages & platforms
Unicode C

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkAiW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkAiW ai;
    HCkJsonObjectW askParams;
    HCkBinDataW bdImageData;

    success = FALSE;

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

    ai = CkAiW_Create();

    CkAiW_putProvider(ai,L"openai");

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

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

    askParams = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(askParams,L"image.output_format",L"jpeg");
    CkJsonObjectW_UpdateString(askParams,L"image.size",L"1024x1024");
    CkJsonObjectW_UpdateString(askParams,L"image.quality",L"low");
    CkAiW_SetAskParams(ai,askParams);

    CkAiW_InputAddText(ai,L"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 = CkAiW_Ask(ai,L"image");
    if (success == FALSE) {
        wprintf(L"%s\n",CkAiW_lastErrorText(ai));
        CkAiW_Dispose(ai);
        CkJsonObjectW_Dispose(askParams);
        return;
    }

    // Get the image response data.
    bdImageData = CkBinDataW_Create();
    success = CkAiW_GetOutputBd(ai,bdImageData);
    if (success == FALSE) {
        wprintf(L"%s\n",CkAiW_lastErrorText(ai));
        CkAiW_Dispose(ai);
        CkJsonObjectW_Dispose(askParams);
        CkBinDataW_Dispose(bdImageData);
        return;
    }

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

    wprintf(L"Success.\n");


    CkAiW_Dispose(ai);
    CkJsonObjectW_Dispose(askParams);
    CkBinDataW_Dispose(bdImageData);

    }