Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.IO;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(GenAIFixture))]
public class ImageGenMultimodalFlashEditImgWithTxtImgTest
{
private readonly GenAIFixture _fixture;
private readonly ImageGenMultimodalFlashEditImgWithTxtImg _sample;

public ImageGenMultimodalFlashEditImgWithTxtImgTest(GenAIFixture fixture)
{
_fixture = fixture;
_sample = new ImageGenMultimodalFlashEditImgWithTxtImg();
}

[Fact]
public async Task TestImageGenMultimodalFlashEditImgWithTxtImg()
{
string imageFilePath = Path.GetFullPath("../../../Resources/example-image-eiffel-tower.png");
FileInfo response = await _sample.GenerateContent(projectId: _fixture.ProjectId, localImageFilePath: imageFilePath);
Assert.NotNull(response);
Assert.True(response.Length > 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.IO;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(GenAIFixture))]
public class ImageGenMultimodalMmFlashTxtAndImgWithTxtTest
{
private readonly GenAIFixture _fixture;
private readonly ImageGenMultimodalFlashTxtAndImgWithTxt _sample;

public ImageGenMultimodalMmFlashTxtAndImgWithTxtTest(GenAIFixture fixture)
{
_fixture = fixture;
_sample = new ImageGenMultimodalFlashTxtAndImgWithTxt();
}

[Fact]
public async Task TestImageGenMultimodalFlashTxtAndImgWithTxt()
{
FileInfo response = await _sample.GenerateContent(_fixture.ProjectId);
Assert.NotNull(response);
Assert.True(response.Length > 0);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]

using Google.GenAI;
using Google.GenAI.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class ImageGenMultimodalFlashEditImgWithTxtImg
{
public async Task<FileInfo> GenerateContent(
string projectId = "your-project-id",
string location = "global",
string model = "gemini-2.5-flash-image",
string localImageFilePath = "path/to/local_image.png")
{
await using var client = new Client(project: projectId, location: location, vertexAI: true);

byte[] imageBytes = File.ReadAllBytes(localImageFilePath);

GenerateContentResponse response = await client.Models.GenerateContentAsync(
model: model,
contents: new List<Content>
{
new Content
{
Role= "user",
Parts = new List<Part>
{
new Part { InlineData = new Blob { Data = imageBytes, MimeType = "image/png" } },
new Part { Text = "Edit this image to make it look like a cartoon."}
}
}
},
config: new GenerateContentConfig { ResponseModalities = new List<string> { "TEXT", "IMAGE" } });

List<Part> parts = response.Candidates?[0]?.Content?.Parts ?? new List<Part>();

var outputFilename = "bw-example-image.png";

foreach (Part part in parts)
{
if (!string.IsNullOrEmpty(part.Text))
{
Console.WriteLine(part.Text);
}
else if (part.InlineData?.Data != null)
{
File.WriteAllBytes(outputFilename, part.InlineData.Data);
}
}

FileInfo fileInfo = new FileInfo(Path.GetFullPath(outputFilename));
Console.WriteLine($"Created output image using {fileInfo.Length} bytes");
// Example response:
// Here's the image cartoonized for you!
// Created output image using 1628165 bytes
return fileInfo;
}
}
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// [START googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]

using Google.GenAI;
using Google.GenAI.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class ImageGenMultimodalFlashTxtAndImgWithTxt
{
public async Task<FileInfo> GenerateContent(
string projectId = "your-project-id",
string location = "global",
string model = "gemini-2.5-flash-image")
{
await using var client = new Client(project: projectId, location: location, vertexAI: true);

GenerateContentResponse response = await client.Models.GenerateContentAsync(
model: model,
contents: new List<Content>
{
new Content
{
Role= "user",
Parts = new List<Part>
{
new Part { Text = "Generate an illustrated recipe for a paella." },
new Part { Text = "Create images to go alongside the text as you generate the recipe."}
}
}
},
config: new GenerateContentConfig { ResponseModalities = new List<string> { "TEXT", "IMAGE" } });

List<Part> parts = response.Candidates?[0]?.Content?.Parts ?? new List<Part>();

using (StreamWriter writer = new StreamWriter("paella-recipe.md"))
{
int imageCounter = 1;
foreach (Part part in parts)
{
if (!string.IsNullOrEmpty(part.Text))
{
writer.WriteLine(part.Text);
}
else if (part.InlineData?.Data != null)
{
string filename = $"example-image-{imageCounter}.png";
File.WriteAllBytes(filename, part.InlineData.Data);
writer.WriteLine($"\n![image]({filename})\n");
imageCounter++;
}
}
}

FileInfo fileInfo = new FileInfo(Path.GetFullPath("paella-recipe.md"));
Console.WriteLine($"Created output image using {fileInfo.Length} bytes");
// Example response:
// Created output image using 2329 bytes
return fileInfo;
}
}
// [END googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]