C#使用FreeImage包处理图片

2022/09 24 18:09

FreeImage较牛X之处在于支持的图片格式多

http://freeimage.sourceforge.net/

使用NuGet添加FreeImage-dotnet-core

FreeImage 使用句柄+API的方式处理

using FreeImageAPI;

private static bool ReduceImage(string filename, int maxSize)
{
    try
    {
        System.IO.FileInfo fi = new System.IO.FileInfo(filename);
        if (!fi.Exists)
        {
            return false;
        }

        fi.Attributes = System.IO.FileAttributes.Normal;
        var fileType = FreeImage.GetFileType(filename, filename.Length);
        var obj = FreeImage.Load(fileType, filename, FREE_IMAGE_LOAD_FLAGS.DEFAULT);
        if (obj.IsNull)
        {
            return false;
        }

        var nobj = FreeImage.Rescale(obj, w, h, FREE_IMAGE_FILTER.FILTER_BILINEAR);
        FreeImage.Save(fileType, nobj, filename, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
        return true;
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
        return false;
    }
}

额外推荐:

https://imagemagick.org/

http://www.graphicsmagick.org/