Google's Imagen and Veo 2 with C# .Net
Generative AI is transforming how we create digital content. Google is at the forefront with powerful models like Imagen for stunning image generation and Veo 2 for creating high-quality, cinematic videos from text prompts. If you're a .Net developer looking to integrate these capabilities into your applications, this guide will show you how to get started using Google's Image and Video generative models. What are Imagen and Veo 2? Imagen: Imagen is a state-of-the-art text-to-image diffusion model that excels at generating high-quality, photorealistic images from textual descriptions. Its ability to understand complex prompts and produce diverse and imaginative visuals makes it an invaluable tool for creatives, developers, and anyone looking to bring their ideas to life. Veo 2: Google's state-of-the-art text-to-video generation model. It aims to create high-definition, coherent videos in various cinematic and visual styles, exceeding a minute in length. Veo 2 excels at understanding prompts, rendering realistic movement, and maintaining consistency across shots. It's currently available in Private Preview via Vertex AI. Prerequisites Before you dive into the code, make sure you have the following: API Keys / Authentication: For Imagen (via Google AI): You'll likely need an API key generated from Google AI Studio or your Google Cloud Console. For Veo 2 (via Vertex AI): You need to authenticate using Application Default Credentials (ADC) or a service account key file (.json). Your Google Cloud project must also be allowlisted for the Veo 2 private preview. Required NuGet Packages: For this tutorial we will be using Google_GenerativeAI SDK dotnet add package Google.Google_GenerativeAI dotnet add package Google.Google_GenerativeAI.Auth Imagen: Bringing Text to Life with Stunning Images The Google_GenerativeAI SDK simplifies the interaction with Imagen, allowing you to focus on your creative vision rather than the underlying API intricacies. Here's a step-by-step guide: 1. Initialize the Google AI Client: Create an instance of the GoogleAi client using your API key. This single line of code, powered by the SDK, manages the authentication and connection setup. var googleAi = new GoogleAi(apiKey); 2. Create the Imagen Model Instance: Instantiate the Imagen model using your desired model name. The SDK handles the model selection and configuration. var imageModel = googleAi.CreateImageModel("imagen-3.0-generate-002"); 3. Generate Images: Use the GenerateImagesAsync method to generate images from a text prompt. The SDK abstracts the complex request formatting, allowing you to simply provide the prompt. var prompt = "A peaceful forest clearing at sunrise"; var response = await imageModel.GenerateImagesAsync(prompt); 4. Process the Response: The response will contain the generated images in base64 encoded strings. The SDK provides easy access to the image data, simplifying further processing. foreach (var image in response. Predictions) { //Save or Display Images //Convert To Bytes var bytes = Convert.FromBase64String(image.BytesBase64Encoded); await File.WriteAllBytesAsync(filePath,bytes); } Veo 2: Crafting Dynamic Videos from Text Veo 2 takes generative AI to the next level by enabling the creation of high-quality videos from text prompts. This powerful tool allows users to generate dynamic and engaging video content with ease. Getting Started with Veo 2 and Vertex AI Currently, Veo 2 is in private preview, meaning access is limited. To use Veo 2, you'll need to request allowlisting for your Vertex AI project. The SDK, in conjunction with Vertex AI, simplifies the interaction with the Veo 2 model. 1. Request Allowlisting: Contact Google Cloud support to request allowlisting for your Vertex AI project. 2. Set Up Vertex AI: Once your project is allowlisted, you'll need to set up Vertex AI and authenticate using a service account. The SDK facilitates this authentication process. //Your preferred authentication mode var authenticator = new GoogleServiceAccountAuthenticator(serviceAccountJsonFile) //Initialize VertexAI instance var vertexAI = new VertexAI(projectId, region, authenticator:authenticator); 3. Create a Video Generation Model Instance: Instantiate the VideoGenerationModel using the Veo 2 model name. The SDK simplifies model instantiation. var veoModelId = VertexAIModels.Video.Veo2Generate001; var model = vertexAI.CreateVideoGenerationModel(veoModelId); 4. Create a GenerateVideosRequest: Define the video generation request, including the prompt, configuration, and other parameters. The SDK helps in creating and formatting the request. var request = new GenerateVideosRequest() { Model = "veo2-generate-001", Prompt = "A dog catching a ball", Config = new GenerateVideosConfig() { AspectRatio = VideoAspec

Generative AI is transforming how we create digital content. Google is at the forefront with powerful models like Imagen for stunning image generation and Veo 2 for creating high-quality, cinematic videos from text prompts.
If you're a .Net developer looking to integrate these capabilities into your applications, this guide will show you how to get started using Google's Image and Video generative models.
What are Imagen and Veo 2?
Imagen: Imagen is a state-of-the-art text-to-image diffusion model that excels at generating high-quality, photorealistic images from textual descriptions. Its ability to understand complex prompts and produce diverse and imaginative visuals makes it an invaluable tool for creatives, developers, and anyone looking to bring their ideas to life.
Veo 2: Google's state-of-the-art text-to-video generation model. It aims to create high-definition, coherent videos in various cinematic and visual styles, exceeding a minute in length. Veo 2 excels at understanding prompts, rendering realistic movement, and maintaining consistency across shots. It's currently available in Private Preview via Vertex AI.
Prerequisites
Before you dive into the code, make sure you have the following:
API Keys / Authentication:
For Imagen (via Google AI): You'll likely need an API key generated from Google AI Studio or your Google Cloud Console.
For Veo 2 (via Vertex AI): You need to authenticate using Application Default Credentials (ADC) or a service account key file (.json). Your Google Cloud project must also be allowlisted for the Veo 2 private preview.
Required NuGet Packages:
For this tutorial we will be using Google_GenerativeAI SDK
dotnet add package Google.Google_GenerativeAI
dotnet add package Google.Google_GenerativeAI.Auth
Imagen: Bringing Text to Life with Stunning Images
The Google_GenerativeAI SDK simplifies the interaction with Imagen, allowing you to focus on your creative vision rather than the underlying API intricacies. Here's a step-by-step guide:
1. Initialize the Google AI Client:
Create an instance of the GoogleAi
client using your API key. This single line of code, powered by the SDK, manages the authentication and connection setup.
var googleAi = new GoogleAi(apiKey);
2. Create the Imagen Model Instance:
Instantiate the Imagen model using your desired model name. The SDK handles the model selection and configuration.
var imageModel = googleAi.CreateImageModel("imagen-3.0-generate-002");
3. Generate Images:
Use the GenerateImagesAsync
method to generate images from a text prompt. The SDK abstracts the complex request formatting, allowing you to simply provide the prompt.
var prompt = "A peaceful forest clearing at sunrise";
var response = await imageModel.GenerateImagesAsync(prompt);
4. Process the Response:
The response will contain the generated images in base64 encoded strings. The SDK provides easy access to the image data, simplifying further processing.
foreach (var image in response. Predictions)
{
//Save or Display Images
//Convert To Bytes
var bytes = Convert.FromBase64String(image.BytesBase64Encoded);
await File.WriteAllBytesAsync(filePath,bytes);
}
Veo 2: Crafting Dynamic Videos from Text
Veo 2 takes generative AI to the next level by enabling the creation of high-quality videos from text prompts. This powerful tool allows users to generate dynamic and engaging video content with ease.
Getting Started with Veo 2 and Vertex AI
Currently, Veo 2 is in private preview, meaning access is limited. To use Veo 2, you'll need to request allowlisting for your Vertex AI project. The SDK, in conjunction with Vertex AI, simplifies the interaction with the Veo 2 model.
1. Request Allowlisting:
Contact Google Cloud support to request allowlisting for your Vertex AI project.
2. Set Up Vertex AI:
Once your project is allowlisted, you'll need to set up Vertex AI and authenticate using a service account. The SDK facilitates this authentication process.
//Your preferred authentication mode
var authenticator = new GoogleServiceAccountAuthenticator(serviceAccountJsonFile)
//Initialize VertexAI instance
var vertexAI = new VertexAI(projectId, region, authenticator:authenticator);
3. Create a Video Generation Model Instance:
Instantiate the VideoGenerationModel
using the Veo 2 model name. The SDK simplifies model instantiation.
var veoModelId = VertexAIModels.Video.Veo2Generate001;
var model = vertexAI.CreateVideoGenerationModel(veoModelId);
4. Create a GenerateVideosRequest:
Define the video generation request, including the prompt, configuration, and other parameters. The SDK helps in creating and formatting the request.
var request = new GenerateVideosRequest()
{
Model = "veo2-generate-001",
Prompt = "A dog catching a ball",
Config = new GenerateVideosConfig()
{
AspectRatio = VideoAspectRatio.LANDSCAPE_16_9,
DurationSeconds = 555,
EnhancePrompt = true,
Fps = 24,
NumberOfVideos = 1,
PersonGeneration = VideoPersonGeneration.AllowAdult,
Resolution = VideoResolution.HD_720P
}
};
5. Generate Videos:
Use the GenerateVideosAsync
method to initiate video generation. The SDK handles the API call and request management.
var operation = await model.GenerateVideosAsync(request);
6. Wait for the Video Generation Completion:
Video generation can take time, so you'll need to wait for the operation to complete. The SDK simplifies the handling of long-running operations.
var response = await model.AwaitForLongRunningOperation(operation.Name, (int)TimeSpan.FromMinutes(10).TotalMilliseconds).ConfigureAwait(false);
7. Process the Response:
Once the operation is complete, you can retrieve the generated video. The SDK provides easy access to the video data.
if (response.Done == true)
{
await File.WriteAllBytesAsync("generated.mp4", response.Result.GeneratedVideos[0].Video.VideoBytes);
}
Conclusion
The Google_GenerativeAI SDK significantly simplifies the process of integrating Imagen and Veo 2 into your applications. By abstracting away the complexities of API interactions and providing intuitive interfaces, the SDK empowers developers to focus on their creative vision. By following the steps outlined in this guide, you can start exploring the capabilities of these models and unlock new possibilities for your creative projects. As Google continues to advance these technologies, we can expect even more exciting developments in the future. Remember that Veo 2 is currently in private preview, so ensure you apply for allowlisting to access its features.
Happy Coding!