11using System ;
2- using System . Diagnostics ;
32using System . IO ;
43using System . Threading ;
54using System . Threading . Tasks ;
@@ -30,8 +29,6 @@ public static class ImageFileOptimization
3029 /// <returns></returns>
3130 public static async Task < ImageFileOptimizationResult > OptimizeImageFileAsync ( ImageFileOptimizationContext context , bool useAreaSizeLimit = true , bool copyNewFile = true )
3231 {
33- var stopwatch = Stopwatch . StartNew ( ) ;
34-
3532 var imageFile = context . ImageFile ;
3633 var workingFolder = context . WorkingFolder ;
3734 var maxImageWidth = context . MaxImageWidth ;
@@ -65,10 +62,6 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
6562 } ;
6663 }
6764
68- stopwatch . Stop ( ) ;
69- context . LogMessage ( $ "Copy new file Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
70- stopwatch . Restart ( ) ;
71-
7265 if ( IsExtension ( ".svg" ) )
7366 {
7467 // 如果是 svg 那就直接转换了,因为后续叠加特效等逻辑都不能支持 SVG 格式
@@ -103,9 +96,6 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
10396 IsExtension ( ".emf" ) )
10497 {
10598 var result = EnhancedGraphicsMetafileOptimization . ConvertWmfOrEmfToPngFile ( context ) ;
106- stopwatch . Stop ( ) ;
107- context . LogMessage ( $ "ConvertWmfOrEmfToPngFile Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
108-
10999 if ( result . OptimizedImageFile is not null )
110100 {
111101 context . LogMessage ( $ "Success ConvertWmfOrEmfToPngFile. Update current image file to '{ result . OptimizedImageFile } '") ;
@@ -120,7 +110,6 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
120110 }
121111 }
122112
123- stopwatch . Restart ( ) ;
124113 context . LogMessage ( $ "Start optimize image with ImageSharp. ImageFile: '{ context . ImageFile . FullName } '") ;
125114
126115 Image < Rgba32 > image ;
@@ -130,9 +119,6 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
130119 FileShare . Read ) ;
131120
132121 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 } '") ;
136122 }
137123 catch ( ImageFormatException e )
138124 {
@@ -179,25 +165,16 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
179165 } ;
180166 }
181167
182- stopwatch . Restart ( ) ;
183168 OptimizeImage ( image , maxImageWidth , maxImageHeight , useAreaSizeLimit ) ;
184- stopwatch . Stop ( ) ;
185- context . LogMessage (
186- $ "Optimize image with ImageSharp Cost { stopwatch . ElapsedMilliseconds } ms. ImageFile: '{ context . ImageFile . FullName } '") ;
187169
188170 // 重新保存即可
189171 var outputImageFilePath = Path . Join ( workingFolder . FullName , $ "{ Path . GetRandomFileName ( ) } .png") ;
190- stopwatch . Restart ( ) ;
191172 await image . SaveAsPngAsync ( outputImageFilePath , new PngEncoder ( )
192173 {
193174 ColorType = PngColorType . RgbWithAlpha ,
194175 BitDepth = PngBitDepth . Bit8 ,
195176 } ) ;
196177
197- stopwatch . Stop ( ) ;
198- context . LogMessage (
199- $ "Save optimized image with ImageSharp Cost { stopwatch . ElapsedMilliseconds } ms. OutputImageFile: '{ outputImageFilePath } '") ;
200-
201178 return new ImageFileOptimizationResult ( )
202179 {
203180 Image = image ,
@@ -336,27 +313,11 @@ public static void LimitImageSize(Image<Rgba32> image, int? maxImageWidth, int?
336313 var imageFile = context . ImageFile ;
337314 var workingFolder = context . WorkingFolder ;
338315
339- var stopwatch = Stopwatch . StartNew ( ) ;
340-
341-
342316 using var skSvg = new SKSvg ( ) ;
343317 using var skPicture = skSvg . Load ( imageFile . FullName ) ;
344-
345- stopwatch . Stop ( ) ;
346- context . LogMessage ( $ "Load sk svg Cost { stopwatch . ElapsedMilliseconds } ms") ;
347-
348318 var outputFile = Path . Join ( workingFolder . FullName ,
349319 $ "SVG_{ Path . GetRandomFileName ( ) } .png") ;
350-
351- stopwatch . Restart ( ) ;
352320 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-
360321 if ( canSave && File . Exists ( outputFile ) )
361322 {
362323 return new FileInfo ( outputFile ) ;
0 commit comments