在 Windows 上,Microsoft Foundry 支持的 AI 映像生成功能通过一组由人工智能助力的、由 Stable Diffusion 驱动的 API 提供,这些 API 作为 Windows 应用 SDK 的一部分发布。 这些 API 可用于 Windows 应用,以使用自然语言提示和设备生成模型创建、转换和增强图像和照片。
AI 映像生成针对 Windows Copilot+ 电脑上的效率和性能进行了优化。
有关 API 详细信息,请参阅 人工智能图像功能的API参考。
先决条件
- Windows 版本: Windows 11 版本 24H2(内部版本 26100)或更高版本
- WinAppSDK 版本:版本 2.0 实验版(2.0.0-Experimental3)
- 硬件: 建议使用已启用 NPU 的电脑
如何使用 AI 映像生成?
使用 AI 图像生成将提示转换为视觉项目。 支持的功能包括:
文本到图像
从描述性文本提示生成图像。 适用于插图、设计、自定义背景和概念可视化效果。
图像转图像
基于文本指南转换现有图像,同时保留结构。 可用于设置样式、主题和其他变体。
Magic Fill
使用 AI 生成的内容填充图像的屏蔽区域。 可用于删除对象、修复区域和直观编辑(通过文本提示而不是手动工具进行复杂的修订)。
着色书样式
将图像转换为可用于着色书或类似教育体验的简化轮廓。
Restyle
将艺术或视觉样式应用于现有图像,同时保留结构。 适用于创意筛选器、艺术模式或主题转换。
例子
使用 AI 映像生成 API 时,请遵循以下基本步骤。
- 确保模型已准备好使用 EnsureReadyAsync。
- 创建 ImageGenerator 实例。
- 选择适当的生成工作流(文本提示、图像输入或掩码)。
- 调用相应的生成方法。
- 以 ImageBuffer 的形式接收输出,以便查看、编辑或保存。
从文本提示生成图像(文本转图像)
此示例演示如何从文本提示生成图像。 具体而言,“美丽的日落映照在山湖上”。
using Microsoft.Windows.AI.Imaging;
using Microsoft.Graphics.Imaging;
public async Task GenerateImageFromText()
{
// Check if models are ready
var readyState = ImageGenerator.GetReadyState();
if (readyState != AIFeatureReadyState.Ready)
{
// Download models if needed
var result = await ImageGenerator.EnsureReadyAsync();
if (result.Status != AIFeatureReadyResultState.Success)
{
Console.WriteLine("Failed to prepare models");
return;
}
}
// Create ImageGenerator instance
using var generator = await ImageGenerator.CreateAsync();
// Configure generation options
var options = new ImageGenerationOptions
{
MaxInferenceSteps = 6,
Creativity = 0.8,
Seed = 42
};
// Generate image
var result = generator.GenerateImageFromTextPrompt("A beautiful sunset over a mountain lake", options);
if (result.Status == ImageGeneratorResultStatus.Success)
{
var imageBuffer = result.Image;
// Use the generated image (save to file, display, etc.)
await SaveImageBufferAsync(imageBuffer, "generated_image.png");
}
else
{
Console.WriteLine($"Image generation failed: {result.Status}");
}
}
转换图像样式(图像到图像)
此示例演示如何根据文本提示将照片转换为油画。 具体来说,“油画风格,粗犷的笔触,艺术性”。
public async Task RestyleImage()
{
using var generator = await ImageGenerator.CreateAsync();
// Load input image
var inputImage = await LoadImageBufferAsync("photo.jpg");
var options = new ImageGenerationOptions();
var styleOptions = new ImageFromImageGenerationOptions
{
Style = ImageFromImageGenerationStyle.Restyle,
ColorPreservation = 0.7f
};
var result = generator.GenerateImageFromImageBuffer(
inputImage,
"oil painting style, thick brush strokes, artistic",
options,
styleOptions);
if (result.Status == ImageGeneratorResultStatus.Success)
{
await SaveImageBufferAsync(result.Image, "restyled_image.png");
}
}
转换图像样式(图像到图像复杂)
此示例演示如何根据文本提示将照片转换为油画。 具体来说,“油画,厚重笔触,丰富的调色板,传统的画布纹理,逼真的光照效果,古典美术风格,多层次的涂料,高度细节,充满戏剧性的对比度,厚涂,纹理画布”。
using Microsoft.Windows.AI.Imaging;
public async Task CreateImageFromPrompt()
{
using ImageGenerator model = await ImageGenerator.CreateAsync();
// Using default values
var options = new ImageGenerationOptions();
// Set ImageFromImageGenerationOptions fields
var imageFromImageOptions = new ImageFromImageGenerationOptions();
imageFromImageOptions.Style = ImageFromImageGenerationStyle.Restyle;
imageFromImageOptions.ColorPreservation = 0.5f; // range [0.0f, 1.0f]
// Load an input image buffer
using var inputImage = await Utils.LoadSampleImageBufferAsync("sdxl_input_horse.png");
var textPrompt = "An oil painting, thick brush strokes, rich color palette, traditional canvas texture, realistic lighting, classical fine art style, layered paint, high detail, dramatic contrast, impasto, textured canvas";
var result = model.GenerateImageFromImageBuffer(inputImage, textPrompt, options, imageFromImageOptions);
if (result.Status == ImageGeneratorResultStatus.Success)
{
// Image generated successfully
var imageBuffer = result.Image;
// Process the imageBuffer as needed, e.g., save to file or display
}
else
{
// Handle error cases based on result.Status
Console.WriteLine($"Image generation failed with status: {result.Status}");
}
}
具有掩码的 Magic 填充
此示例演示如何使用掩码填充图像的区域。 具体来说,是“一辆红色跑车”。
public async Task FillMaskedRegion()
{
using var generator = await ImageGenerator.CreateAsync();
var inputImage = await LoadImageBufferAsync("scene.jpg");
var maskImage = await LoadImageBufferAsync("mask.png"); // GRAY8 format
var options = new ImageGenerationOptions();
var result = generator.GenerateImageFromImageBufferAndMask(
inputImage,
maskImage,
"a red sports car",
options);
if (result.Status == ImageGeneratorResultStatus.Success)
{
await SaveImageBufferAsync(result.Image, "filled_image.png");
}
}
生成着色书样式图像
此示例演示如何在着色书样式中生成图像。 具体而言,一只“宇宙飞船中的猫”。
using Microsoft.Windows.AI.Imaging;
public async Task CreateImageFromPrompt()
{
using ImageGenerator model = await ImageGenerator.CreateAsync();
// Using default values
var options = new ImageGenerationOptions();
// Set ImageFromTextGenerationOptions fields
var imageFromTextOptions = new ImageFromTextGenerationOptions();
imageFromTextOptions.Style = ImageFromTextGenerationStyle.ColoringBook;
var result = model.GenerateImageFromTextPrompt("Cat in spaceship", options, imageFromTextOptions);
if (result.Status == ImageGeneratorResultStatus.Success)
{
// Image generated successfully
var imageBuffer = result.Image;
// Process the imageBuffer as needed, e.g., save to file or display
}
else
{
// Handle error cases based on result.Status
Console.WriteLine($"Image generation failed with status: {result.Status}");
}
}
使用自定义 ImageGenerationOptions 参数生成映像
此示例演示如何基于一组内容筛选器和限制生成图像。 具体而言,使用 TextContentFilterSeverity 级别为 Low 和 ImageContentFilterSeverity 级别为 Minimum 的“Cat in spaceship”。
using Microsoft.Windows.AI.Imaging;
using Microsoft.Windows.AI.ContentSafety;
public async Task CreateImageFromPromptAndCustomOptions()
{
using ImageGenerator model = await ImageGenerator.CreateAsync();
// Using default values
var options = new ImageGenerationOptions();
// Set custom ImageGenerationOptions fields
options.MaxInferenceSteps = 6;
options.Creativity = 0.8;
options.Seed = 1234;
ContentFilterOptions contentFilterOptions = new ContentFilterOptions();
contentFilterOptions.PromptMaxAllowedSeverityLevel = TextContentFilterSeverity(SeverityLevel.Low);
contentFilterOptions.ImageMaxAllowedSeverityLevel = ImageContentFilterSeverity(SeverityLevel.Minimium);
options.ContentFilterOptions = contentFilterOptions;
var result = model.GenerateImageFromTextPrompt("Cat in spaceship", options);
if (result.Status == ImageGeneratorResultStatus.Success)
{
// Image generated successfully
var imageBuffer = result.Image;
// Process the imageBuffer as needed, e.g., save to file or display
}
else
{
// Handle error cases based on result.Status
Console.WriteLine($"Image generation failed with status: {result.Status}");
}
}
负责任的人工智能
我们已使用以下步骤的组合来确保这些映像 API 可信、安全且负责任地生成。 我们建议在应用中实施 AI 功能时,参阅 Windows 上负责任的生成式 AI 开发中描述的最佳做法。