Skip to content

Latest commit

 

History

History
77 lines (57 loc) · 2.07 KB

README.md

File metadata and controls

77 lines (57 loc) · 2.07 KB

ThumbHashCSharp

这是一个 ThumbHash 的 C# 实现。ThumbHash 是一种生成图像缩略图(通常使用base64存储)的算法,具体原理见:ThumbHash 原理。 这个算法很适合用来制作图片加载的占位符

这个库的特别之处

!支持100*100以上大小的图片 !默认使用base64或者byte[]输出转码

安装

你可以通过 NuGet 安装 ThumbHashSharp:

dotnet add package ThumbHashSharp

使用方法

以下是如何使用 ThumbHashSharp 的一些示例:

using ThumbHashSharp;

// 将 RGBA 图像编码为 ThumbHash
int width = 100;
int height = 100;
ReadOnlySpan<byte> rgba = ...; // 你的 RGBA 数据
string thumbHashBase64 = ThumbHashHelper.RgbaToThumbHashBase64(width, height, rgba);

// 将 ThumbHash 解码为 RGBA 图像
Image image = ThumbHashHelper.ThumbHashToRgba(thumbHashBase64);

// 从 ThumbHash 中提取平均颜色
RGBA averageColor = ThumbHashHelper.ThumbHashToAverageRgba(thumbHashBase64);

// 提取原始图像的近似宽高比
float aspectRatio = ThumbHashHelper.ThumbHashToApproximateAspectRatio(thumbHashBase64);

示例

以下是几个图片前后的效果展示:

示例 1


png图片

示例 2


jpg图片

示例 3


webp图片(因为webp图片的解码问题预览图保存为webp放大效果有问题,保存为其他格式会好一些)

示例 4


avif图片