前言
项目需要保存缩略图,写了个简单的保存代码。
在包控制管理工具中安装Magick.NET-Q16-AnyCPU
using ImageMagick;
代码
根据原图的格式保存缩略图
/// <summary>
/// 保存缩略图
/// </summary>
/// <param name="originalFilePath">原图地址</param>
/// <param name="destinationFilePath">模板地址</param>
/// <param name="targetWidth">目标大小</param>
public static int ResizeAndSaveImage(string originalFilePath, string destinationFilePath, int targetWidth)
{
int flag = 0;
using (MagickImage image = new MagickImage(originalFilePath))
{
if (image.Width > targetWidth)
{
int targetHeight = (int)(((double)targetWidth / image.Width) * image.Height);
image.Resize(targetWidth, targetHeight);
image.Write(destinationFilePath);
flag = 1;
}
else
{
image.Resize(image.Width, image.Height);
image.Write(destinationFilePath);
flag = 1;
}
}
return flag;
}
标签:缩略图,C#,image,保存,int,flag,targetWidth From: https://blog.csdn.net/roc_wei_chen/article/details/140289722