#运行环境:
- .Net 版本:.Net 5
- Magick.NET版本:8.2.1
- 操作系统:Ubuntu 18.04
#示例代码:Resize GIF格式图片
static void Main(string[] args)
{
var filePath = @"/测试资源/图片/gif/龙猫.gif";
var outPath = @"/测试资源/图片/gif/龙猫1.gif";
ResizeImage(outPath,filePath,500,300);
Console.ReadLine();
}
public static bool ResizeImage(string outFilePath, string sourceFilePath, int outWidth, int outtHeight)
{
try
{
using var image = new MagickImage(sourceFilePath);
if (image.Format != MagickFormat.Gif)
{
image.Resize(outWidth, outtHeight);
image.Write(outFilePath);
}
else
{
using var collection = new MagickImageCollection(new FileInfo(sourceFilePath));
collection.Coalesce();
foreach (var item in collection)
{
item.Resize(outWidth, outtHeight);
}
collection.Write(outFilePath);
}
return true;
}
catch (Exception)
{
return false;
}
}