11using System ;
2+ using System . Diagnostics ;
23using System . IO ;
34using System . Threading ;
45using System . Threading . Tasks ;
@@ -29,6 +30,8 @@ public static class ImageFileOptimization
2930 /// <returns></returns>
3031 public static async Task < ImageFileOptimizationResult > OptimizeImageFileAsync ( ImageFileOptimizationContext context , bool useAreaSizeLimit = true , bool copyNewFile = true )
3132 {
33+ var stopwatch = Stopwatch . StartNew ( ) ;
34+
3235 var imageFile = context . ImageFile ;
3336 var workingFolder = context . WorkingFolder ;
3437 var maxImageWidth = context . MaxImageWidth ;
@@ -62,6 +65,10 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
6265 } ;
6366 }
6467
68+ stopwatch . Stop ( ) ;
69+ context . LogMessage ( $ "Copy new file Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
70+ stopwatch . Restart ( ) ;
71+
6572 if ( IsExtension ( ".svg" ) )
6673 {
6774 // 如果是 svg 那就直接转换了,因为后续叠加特效等逻辑都不能支持 SVG 格式
@@ -96,6 +103,9 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
96103 IsExtension ( ".emf" ) )
97104 {
98105 var result = EnhancedGraphicsMetafileOptimization . ConvertWmfOrEmfToPngFile ( context ) ;
106+ stopwatch . Stop ( ) ;
107+ context . LogMessage ( $ "ConvertWmfOrEmfToPngFile Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
108+
99109 if ( result . OptimizedImageFile is not null )
100110 {
101111 context . LogMessage ( $ "Success ConvertWmfOrEmfToPngFile. Update current image file to '{ result . OptimizedImageFile } '") ;
@@ -110,6 +120,7 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
110120 }
111121 }
112122
123+ stopwatch . Restart ( ) ;
113124 context . LogMessage ( $ "Start optimize image with ImageSharp. ImageFile: '{ context . ImageFile . FullName } '") ;
114125
115126 Image < Rgba32 > image ;
@@ -119,6 +130,9 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
119130 FileShare . Read ) ;
120131
121132 image = await Image . LoadAsync < Rgba32 > ( fileStream ) ;
133+
134+ stopwatch . Stop ( ) ;
135+ context . LogMessage ( $ "Load image with ImageSharp Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
122136 }
123137 catch ( ImageFormatException e )
124138 {
@@ -165,16 +179,25 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
165179 } ;
166180 }
167181
182+ stopwatch . Restart ( ) ;
168183 OptimizeImage ( image , maxImageWidth , maxImageHeight , useAreaSizeLimit ) ;
184+ stopwatch . Stop ( ) ;
185+ context . LogMessage (
186+ $ "Optimize image with ImageSharp Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
169187
170188 // 重新保存即可
171189 var outputImageFilePath = Path . Join ( workingFolder . FullName , $ "{ Path . GetRandomFileName ( ) } .png") ;
190+ stopwatch . Restart ( ) ;
172191 await image . SaveAsPngAsync ( outputImageFilePath , new PngEncoder ( )
173192 {
174193 ColorType = PngColorType . RgbWithAlpha ,
175194 BitDepth = PngBitDepth . Bit8 ,
176195 } ) ;
177196
197+ stopwatch . Stop ( ) ;
198+ context . LogMessage (
199+ $ "Save optimized image with ImageSharp Cost { stopwatch . ElapsedMilliseconds } ms. OutputImageFile: '{ outputImageFilePath } '") ;
200+
178201 return new ImageFileOptimizationResult ( )
179202 {
180203 Image = image ,
@@ -313,11 +336,27 @@ public static void LimitImageSize(Image<Rgba32> image, int? maxImageWidth, int?
313336 var imageFile = context . ImageFile ;
314337 var workingFolder = context . WorkingFolder ;
315338
339+ var stopwatch = Stopwatch . StartNew ( ) ;
340+
341+
316342 using var skSvg = new SKSvg ( ) ;
317343 using var skPicture = skSvg . Load ( imageFile . FullName ) ;
344+
345+ stopwatch . Stop ( ) ;
346+ context . LogMessage ( $ "Load sk svg Cost { stopwatch . ElapsedMilliseconds } ms") ;
347+
318348 var outputFile = Path . Join ( workingFolder . FullName ,
319349 $ "SVG_{ Path . GetRandomFileName ( ) } .png") ;
350+
351+ stopwatch . Restart ( ) ;
320352 var canSave = skSvg . Save ( outputFile , SKColors . Transparent ) ;
353+ stopwatch . Stop ( ) ;
354+ context . LogMessage ( $ "Save sk svg to png Cost { stopwatch . ElapsedMilliseconds } ms. OutputFile='{ outputFile } '") ;
355+
356+ //var skBitmap = skPicture.ToBitmap(SKColor.Empty, 1,1,SKColorType.Bgra8888,SKAlphaType.Unpremul,null!);
357+ //var pixelSpan = skBitmap.GetPixelSpan();
358+ //Image<Rgba32>.LoadPixelData<Rgba32>(pixelSpan, skBitmap.Width, skBitmap.Height)
359+
321360 if ( canSave && File . Exists ( outputFile ) )
322361 {
323362 return new FileInfo ( outputFile ) ;
0 commit comments