1+ using DotNetCampus . MediaConverters . Imaging . Effect . Color ;
2+
3+ using SixLabors . ImageSharp ;
4+ using SixLabors . ImageSharp . PixelFormats ;
5+
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . Linq ;
9+ using System . Text ;
10+ using System . Threading . Tasks ;
11+ using DotNetCampus . MediaConverters . Imaging . Effect . Extensions ;
12+
13+ namespace DotNetCampus . MediaConverters . Imaging . Effect ;
14+
15+ /// <summary>
16+ /// 为<see cref="Image{T}"/>提供效果拓展
17+ /// </summary>
18+ public static class BitmapEffectExtension
19+ {
20+ /// <summary>
21+ /// 将图片<paramref name="bitmap"/>上指定的颜色<paramref name="sourceColor"/>替换为颜色<paramref name="targetColor"/>
22+ /// </summary>
23+ /// <param name="bitmap">图片</param>
24+ /// <param name="sourceColor">原始颜色</param>
25+ /// <param name="targetColor">目标颜色</param>
26+ public static void ReplaceColor ( this Image < Rgba32 > bitmap , ColorMetadata sourceColor , ColorMetadata targetColor )
27+ {
28+ bitmap . PerPixelProcess ( color =>
29+ color . IsNearlyEquals ( sourceColor ) ? targetColor : color ) ;
30+ }
31+
32+ /// <summary>
33+ /// 将图片<paramref name="bitmap"/>上指定的一部分颜色替换为指定的对应颜色
34+ /// </summary>
35+ /// <param name="bitmap">图片</param>
36+ /// <param name="colorInfos">存储替换信息的颜色组</param>
37+ public static void ReplaceColor ( this Image < Rgba32 > bitmap , Dictionary < ColorMetadata , ColorMetadata > colorInfos )
38+ {
39+ bitmap . PerPixelProcess ( color =>
40+ {
41+ foreach ( var colorInfo in colorInfos )
42+ {
43+ if ( color . IsNearlyEquals ( colorInfo . Key ) )
44+ {
45+ return colorInfo . Value ;
46+ }
47+ }
48+
49+ return color ;
50+ } ) ;
51+ }
52+
53+ /// <summary>
54+ /// 对图片<paramref name="bitmap"/>设置(DuotoneEffect)双色调效果
55+ /// </summary>
56+ /// <param name="bitmap"></param>
57+ /// <param name="color1"></param>
58+ /// <param name="color2"></param>
59+ public static void SetDuotoneEffect ( this Image < Rgba32 > bitmap , ColorMetadata color1 , ColorMetadata color2 )
60+ {
61+ bitmap . PerPixelProcess ( color => color . GetDuotoneColor ( color1 , color2 ) ) ;
62+ }
63+
64+ /// <summary>
65+ /// 设置黑白图效果
66+ /// </summary>
67+ /// <param name="bitmap"></param>
68+ /// <param name="threshold">像素灰度大于该阈值设为白色,否则为黑色。范围 0-1</param>
69+ public static void SetBlackWhiteEffect ( this Image < Rgba32 > bitmap , float threshold )
70+ {
71+ bitmap . PerPixelProcess ( color =>
72+ {
73+ var v = color . GetGrayScale ( ) >= threshold ? 1 : 0 ;
74+ return new ColorMetadata ( v , v , v , color . ARGB . Item4 ) ;
75+ } ) ;
76+ }
77+
78+ /// <summary>
79+ /// 更改当前图像的亮度。
80+ /// </summary>
81+ /// <param name="bitmap">图片</param>
82+ /// <param name="percentage">转化比例,必须大于或等于 0。
83+ /// <remarks>
84+ /// 值为 0 将创建一个完全黑色的图像。值为 1 时输入保持不变。
85+ /// 其他值是效果的线性乘数。允许超过 1 的值,从而提供更明亮的结果。
86+ /// </remarks>
87+ /// </param>
88+ public static void SetBrightness ( this Image < Rgba32 > bitmap , float percentage )
89+ {
90+ var colorMatrix = ColorMatrices . CreateBrightnessFilter ( percentage ) ;
91+ bitmap . PerPixelProcess ( color => color . ApplyMatrix ( colorMatrix ) ) ;
92+ }
93+
94+ /// <summary>
95+ /// 更改当前图像的对比度。
96+ /// </summary>
97+ /// <param name="bitmap">图片</param>
98+ /// <param name="percentage">转化比例,必须大于或等于 0。
99+ /// <remarks>
100+ /// 值为 0 将创建一个完全灰色的图像。值为 1 时输入保持不变。
101+ /// 其他值是效果的线性乘数。允许超过 1 的值,从而提供具有更高对比度的结果。
102+ /// </remarks>
103+ /// </param>
104+ public static void SetContrast ( this Image < Rgba32 > bitmap , float percentage )
105+ {
106+ var colorMatrix = ColorMatrices . CreateContrastFilter ( percentage ) ;
107+ bitmap . PerPixelProcess ( color => color . ApplyMatrix ( colorMatrix ) ) ;
108+ }
109+
110+ /// <summary>
111+ /// 设置灰度图效果。
112+ /// </summary>
113+ /// <param name="bitmap">图片</param>
114+ public static void SetGrayScaleEffect ( this Image < Rgba32 > bitmap )
115+ {
116+ var colorMatrix = ColorMatrices . CreateGrayScaleFilter ( 1 ) ;
117+ bitmap . PerPixelProcess ( color => color . ApplyMatrix ( colorMatrix ) ) ;
118+ }
119+
120+ /// <summary>
121+ /// 设置冲蚀效果。
122+ /// </summary>
123+ /// <param name="bitmap">图片</param>
124+ public static void SetLuminanceEffect ( this Image < Rgba32 > bitmap )
125+ {
126+ bitmap . SetContrast ( 0.2f ) ;
127+ bitmap . SetBrightness ( 1.9f ) ;
128+ }
129+
130+ /// <summary>
131+ /// 设置柔化边缘效果
132+ /// </summary>
133+ /// <param name="bitmap"></param>
134+ /// <param name="radius"></param>
135+ public static void SetSoftEdgeEffect ( this Image < Rgba32 > bitmap , float radius )
136+ {
137+ //var pixelFormat = bitmap.PixelFormat;
138+ //if (pixelFormat != PixelFormat.Format32bppArgb)
139+ //{
140+ // throw new NotSupportedException($"Only {PixelFormat.Format32bppArgb} image pixel format is supported.");
141+ //}
142+
143+ SoftEdgeHelper . SetSoftEdgeMask ( bitmap , radius ) ;
144+ }
145+ }
0 commit comments