1- using System ;
2- using System . IO ;
3- using System . Linq ;
4- using System . Threading . Tasks ;
5-
6- using SixLabors . ImageSharp ;
1+ using SixLabors . ImageSharp ;
72using SixLabors . ImageSharp . Formats . Gif ;
83using SixLabors . ImageSharp . Formats . Png ;
4+ using SixLabors . ImageSharp . Metadata . Profiles . Exif ;
95using SixLabors . ImageSharp . PixelFormats ;
106using SixLabors . ImageSharp . Processing ;
117
8+ using System ;
9+ using System . IO ;
10+ using System . Linq ;
11+ using System . Threading . Tasks ;
12+
1213namespace DotNetCampus . MediaConverters . Imaging . Optimizations ;
1314
1415public static class ImageFileOptimization
@@ -26,7 +27,7 @@ public static class ImageFileOptimization
2627 /// <param name="useAreaSizeLimit">当包含宽度高度限制时,采用面积限制。采用面积限制时,可能宽度或高度依然超过限制的最大宽度高度。采用面积限制时,可以保证最大像素数量小于限制数量的同时,让图片可以达到最大尺寸</param>
2728 /// <returns></returns>
2829 public static async Task < ImageFileOptimizationResult > OptimizeImageFileAsync ( FileInfo imageFile ,
29- DirectoryInfo workingFolder , int ? maxImageWidth = null , int ? maxImageHeight = null , bool copyNewFile = true , bool useAreaSizeLimit = true )
30+ DirectoryInfo workingFolder , int ? maxImageWidth = null , int ? maxImageHeight = null , bool useAreaSizeLimit = true , bool copyNewFile = true )
3031 {
3132 if ( ! File . Exists ( imageFile . FullName ) )
3233 {
@@ -53,7 +54,6 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Fil
5354 {
5455 await using var fileStream = new FileStream ( file . FullName , FileMode . Open , FileAccess . Read , FileShare . Read ) ;
5556
56- // 额外输出旋转图片的情况
5757 image = await Image . LoadAsync < Rgba32 > ( fileStream ) ;
5858 }
5959 catch ( ImageFormatException e )
@@ -87,17 +87,7 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Fil
8787 } ;
8888 }
8989
90- // 不带点的后缀名
91- //var fileExtension = image.Metadata.DecodedImageFormat?.FileExtensions.FirstOrDefault();
92-
93- if ( useAreaSizeLimit && maxImageWidth is not null && maxImageHeight is not null )
94- {
95- LimitImageSize ( image , maxImageWidth . Value * maxImageHeight . Value ) ;
96- }
97- else
98- {
99- LimitImageSize ( image , maxImageWidth , maxImageHeight ) ;
100- }
90+ OptimizeImage ( image , maxImageWidth , maxImageHeight , useAreaSizeLimit ) ;
10191
10292 // 重新保存即可
10393 var outputImageFilePath = Path . Join ( workingFolder . FullName , $ "{ Path . GetRandomFileName ( ) } .png") ;
@@ -114,6 +104,35 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Fil
114104 } ;
115105 }
116106
107+ /// <summary>
108+ /// 优化图片,包括自动旋转、限制图片尺寸等
109+ /// </summary>
110+ /// <param name="image"></param>
111+ /// <param name="maxImageWidth"></param>
112+ /// <param name="maxImageHeight"></param>
113+ /// <param name="useAreaSizeLimit"></param>
114+ public static void OptimizeImage ( Image < Rgba32 > image , int ? maxImageWidth = null , int ? maxImageHeight = null , bool useAreaSizeLimit = true )
115+ {
116+ // 额外输出旋转图片的情况
117+ //if (image.Metadata.ExifProfile is not null && image.Metadata.ExifProfile.TryGetValue(SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifTag.Orientation,out var value))
118+ //{
119+ //}
120+ // 不需要去解析 Exif 信息,因为 ImageSharp 会自动处理 Exif 信息
121+ image . Mutate ( context => context . AutoOrient ( ) ) ;
122+
123+ // 不带点的后缀名
124+ //var fileExtension = image.Metadata.DecodedImageFormat?.FileExtensions.FirstOrDefault();
125+
126+ if ( useAreaSizeLimit && maxImageWidth is not null && maxImageHeight is not null )
127+ {
128+ LimitImageSize ( image , maxImageWidth . Value * maxImageHeight . Value ) ;
129+ }
130+ else
131+ {
132+ LimitImageSize ( image , maxImageWidth , maxImageHeight ) ;
133+ }
134+ }
135+
117136 /// <summary>
118137 /// 限制图片尺寸。按照面积方式限制,保持比例
119138 /// </summary>
0 commit comments