@@ -87,9 +87,14 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
8787 var context = new ImageFileOptimizationContext ( inputFile , workingFolder , imageConvertContext . MaxImageWidth ,
8888 imageConvertContext . MaxImageHeight )
8989 {
90- PngCompressionLevel = imageConvertContext . PngCompressionLevel ,
90+ // 由于不准备在此步骤保存为文件,所以不需要设置保存等级
91+ //PngCompressionLevel = imageConvertContext.PngCompressionLevel,
92+
9193 ShouldLogToConsole = convertHandler . ShouldLogToConsole ?? false ,
9294 ShouldLogToFile = convertHandler . ShouldLogToFile ?? false ,
95+
96+ // 不要在优化图片步骤时保存为文件,因为后续可能还会进行转换任务。统一一个逻辑执行保存即可
97+ ShouldSaveToPngFile = false ,
9398 } ;
9499
95100 context . LogMessage ( $ "[Performance] FromJsonText cost { stepStopwatch . ElapsedMilliseconds } ms Total { totalStopwatch . ElapsedMilliseconds } ms") ;
@@ -133,8 +138,7 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
133138
134139 return ErrorCode . UnknownError ;
135140 }
136-
137- FileInfo optimizedImageFile = imageFileOptimizationResult . OptimizedImageFile ;
141+
138142 var image = imageFileOptimizationResult . Image ;
139143
140144 if ( image is not null && imageConvertContext . ImageConvertTaskList is { } list )
@@ -148,27 +152,25 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
148152
149153 context . LogMessage ( $ "[Performance] RunWorkerProvider cost { stepStopwatch . ElapsedMilliseconds } ms Total { totalStopwatch . ElapsedMilliseconds } ms") ;
150154 stepStopwatch . Restart ( ) ;
151-
152- await image . SaveAsPngAsync ( convertHandler . OutputFile , new PngEncoder ( )
153- {
154- ColorType = PngColorType . RgbWithAlpha ,
155- BitDepth = PngBitDepth . Bit8 ,
156- // 压缩等级 1-9,数值越大压缩率越高但速度越慢,默认值为 6。在 1-9 范围外的值会被视为默认值
157- CompressionLevel = imageConvertContext . PngCompressionLevel is >= 1 and <= 9
158- ? ( PngCompressionLevel ) imageConvertContext . PngCompressionLevel
159- // 范围外,使用默认值
160- : PngCompressionLevel . DefaultCompression
161- } ) ;
162-
163- context . LogMessage ( $ "[Performance] SaveAsPngAsync cost { stepStopwatch . ElapsedMilliseconds } ms Total { totalStopwatch . ElapsedMilliseconds } ms") ;
164155 }
165156 else
166157 {
167- optimizedImageFile . CopyTo ( convertHandler . OutputFile , overwrite : true ) ;
168-
169- context . LogMessage ( $ "[Performance] CopyTo cost { stepStopwatch . ElapsedMilliseconds } ms Total { totalStopwatch . ElapsedMilliseconds } ms") ;
158+ // 没有任何转换任务,啥都不干
170159 }
171160
161+ await image . SaveAsPngAsync ( convertHandler . OutputFile , new PngEncoder ( )
162+ {
163+ ColorType = PngColorType . RgbWithAlpha ,
164+ BitDepth = PngBitDepth . Bit8 ,
165+ // 压缩等级 1-9,数值越大压缩率越高但速度越慢,默认值为 6。在 1-9 范围外的值会被视为默认值
166+ CompressionLevel = imageConvertContext . PngCompressionLevel is >= 1 and <= 9
167+ ? ( PngCompressionLevel ) imageConvertContext . PngCompressionLevel
168+ // 范围外,使用默认值
169+ : PngCompressionLevel . DefaultCompression
170+ } ) ;
171+
172+ context . LogMessage ( $ "[Performance] SaveAsPngAsync cost { stepStopwatch . ElapsedMilliseconds } ms Total { totalStopwatch . ElapsedMilliseconds } ms") ;
173+
172174 totalStopwatch . Stop ( ) ;
173175 context . LogMessage ( $ "Success converted image. Cost { totalStopwatch . ElapsedMilliseconds } ms. OutputFile:'{ convertHandler . OutputFile } '") ;
174176
0 commit comments