GifTools 1.0.0
dotnet add package GifTools --version 1.0.0
NuGet\Install-Package GifTools -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GifTools" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GifTools" Version="1.0.0" />
<PackageReference Include="GifTools" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GifTools --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: GifTools, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GifTools@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GifTools&version=1.0.0
#tool nuget:?package=GifTools&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
try
{
// ========== 准备测试数据:替换为你自己的图片路径 ==========
// 支持 PNG/JPG/BMP 等常见格式,至少1张
var imagePaths = new List<string>
{
@"D:\test_images\frame1.png",
@"D:\test_images\frame2.jpg",
@"D:\test_images\frame3.bmp"
};
// 基础输出路径(可根据场景调整)
string baseOutputPath = @"D:\test_gifs\";
// ========== 场景1:基础使用(默认参数,16:9尺寸,不压缩) ==========
Console.WriteLine("开始生成【基础版】GIF...");
var gifTool1 = new GifTools();
await gifTool1.ConvertImagesToGif(
imagePaths: imagePaths,
outputGifPath: $"{baseOutputPath}basic_16x9.gif",
sizeMode: ImageSizeMode.Default16x9, // 默认16:9(720x405)
frameDelayMs: 500, // 每帧500ms
loopCount: 0, // 无限循环
clarity: 5, // 中等清晰度
quality: 128, // 中等画质(128色)
imageResizeMode: ResizeMode.Pad, // 填充模式(无裁剪,空白填充)
isCompressGif: false // 不压缩
);
Console.WriteLine("【基础版】GIF生成完成!");
// ========== 场景2:使用第一张图片原始尺寸,裁剪模式,高画质 ==========
Console.WriteLine("开始生成【原图尺寸+高画质】GIF...");
var gifTool2 = new GifTools();
await gifTool2.ConvertImagesToGif(
imagePaths: imagePaths,
outputGifPath: $"{baseOutputPath}original_size_high_quality.gif",
sizeMode: ImageSizeMode.UseFirstImage, // 使用第一张图片的宽高
frameDelayMs: 300, // 更快的播放速度(300ms/帧)
loopCount: 5, // 循环5次后停止
clarity: 9, // 高清晰度(8-10级,16x16抖动)
quality: 255, // 最高画质(255色,接近真彩色)
imageResizeMode: ResizeMode.Crop, // 裁剪模式(填满画布,居中裁剪)
isCompressGif: false // 不压缩
);
Console.WriteLine("【原图尺寸+高画质】GIF生成完成!");
// ========== 场景3:自定义尺寸,BoxPad模式,开启深度压缩 ==========
Console.WriteLine("开始生成【自定义尺寸+压缩版】GIF...");
var gifTool3 = new GifTools();
await gifTool3.ConvertImagesToGif(
imagePaths: imagePaths,
outputGifPath: $"{baseOutputPath}custom_size_compressed.gif",
sizeMode: ImageSizeMode.Custom, // 自定义尺寸
customWidth: 600, // 自定义宽度600
customHeight: 600, // 自定义高度600(正方形)
frameDelayMs: 400,
loopCount: 0,
clarity: 4, // 中低清晰度(优先体积)
quality: 64, // 中低画质(64色)
imageResizeMode: ResizeMode.BoxPad, // BoxPad模式(放大时仅填充,缩小等价Pad)
isCompressGif: true, // 开启压缩
compressScaleRatio: 0.7f, // 缩放到70%尺寸(最有效的压缩)
compressColorCount: 32, // 压缩后仅保留32色
compressFrameSkipStep: 2, // 抽帧(每2帧保留1帧,减少帧数)
compressQualityLevel: 6 // 压缩清晰度6级(平衡)
);
Console.WriteLine("【自定义尺寸+压缩版】GIF生成完成!");
// ========== 场景4:低清小体积,Stretch模式(强制拉伸) ==========
Console.WriteLine("开始生成【低清小体积+拉伸】GIF...");
var gifTool4 = new GifTools();
await gifTool4.ConvertImagesToGif(
imagePaths: imagePaths,
outputGifPath: $"{baseOutputPath}low_size_stretch.gif",
sizeMode: ImageSizeMode.Default16x9,
frameDelayMs: 200, // 快速播放
loopCount: 0,
clarity: 1, // 最低清晰度(关闭抖动)
quality: 16, // 极低画质(仅16色)
imageResizeMode: ResizeMode.Stretch, // 拉伸模式(强制适配尺寸,可能变形)
isCompressGif: true, // 开启压缩
compressScaleRatio: 0.5f, // 缩放到50%
compressColorCount: 8, // 仅8色
compressFrameSkipStep: 3, // 每3帧保留1帧
compressQualityLevel: 2 // 压缩清晰度2级(最快)
);
Console.WriteLine("【低清小体积+拉伸】GIF生成完成!");
// ========== 场景5:单独调用CompressGif方法压缩已有GIF ==========
Console.WriteLine("开始压缩已有GIF...");
var gifTool5 = new GifTools();
// 压缩场景1生成的基础版GIF
gifTool5.CompressGif(
inputPath: $"{baseOutputPath}basic_16x9.gif",
outputPath: $"{baseOutputPath}basic_16x9_compressed.gif",
scaleRatio: 0.8f, // 缩放到80%
colorCount: 64, // 64色
frameSkipStep: 1, // 不抽帧
qualityLevel: 7 // 中等清晰度
);
Console.WriteLine("已有GIF压缩完成!");
Console.WriteLine("\n所有GIF生成/压缩任务完成!");
}
catch (ArgumentException ex)
{
// 捕获参数校验异常
Console.WriteLine($"参数错误:{ex.Message}");
}
catch (FileNotFoundException ex)
{
// 捕获文件不存在异常
Console.WriteLine($"文件不存在:{ex.FileName} → {ex.Message}");
}
catch (InvalidOperationException ex)
{
// 捕获压缩失败等运行时异常
Console.WriteLine($"运行时错误:{ex.Message}");
}
catch (Exception ex)
{
// 捕获其他未知异常
Console.WriteLine($"未知错误:{ex.Message}\n{ex.StackTrace}");
}
finally
{
Console.WriteLine("\n按任意键退出...");
Console.ReadKey();
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- SixLabors.ImageSharp (>= 3.1.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 117 | 3/2/2026 |