@@ -78,32 +78,40 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Fil
7878 } ;
7979 }
8080
81- using var _ = image ;
82-
83- if ( image . Metadata . DecodedImageFormat is GifFormat )
81+ try
8482 {
85- return new ImageFileOptimizationResult ( )
83+ if ( image . Metadata . DecodedImageFormat is GifFormat )
8684 {
87- OptimizedImageFile = null ,
88- FailureReason = ImageFileOptimizationFailureReason . NotSupported
89- } ;
90- }
85+ image . Dispose ( ) ;
86+ return new ImageFileOptimizationResult ( )
87+ {
88+ OptimizedImageFile = null ,
89+ FailureReason = ImageFileOptimizationFailureReason . NotSupported
90+ } ;
91+ }
9192
92- OptimizeImage ( image , maxImageWidth , maxImageHeight , useAreaSizeLimit ) ;
93+ OptimizeImage ( image , maxImageWidth , maxImageHeight , useAreaSizeLimit ) ;
9394
94- // 重新保存即可
95- var outputImageFilePath = Path . Join ( workingFolder . FullName , $ "{ Path . GetRandomFileName ( ) } .png") ;
96- await image . SaveAsPngAsync ( outputImageFilePath , new PngEncoder ( )
97- {
98- ColorType = PngColorType . RgbWithAlpha ,
99- BitDepth = PngBitDepth . Bit8 ,
100- } ) ;
95+ // 重新保存即可
96+ var outputImageFilePath = Path . Join ( workingFolder . FullName , $ "{ Path . GetRandomFileName ( ) } .png") ;
97+ await image . SaveAsPngAsync ( outputImageFilePath , new PngEncoder ( )
98+ {
99+ ColorType = PngColorType . RgbWithAlpha ,
100+ BitDepth = PngBitDepth . Bit8 ,
101+ } ) ;
101102
102- return new ImageFileOptimizationResult ( )
103+ return new ImageFileOptimizationResult ( )
104+ {
105+ Image = image ,
106+ OptimizedImageFile = new FileInfo ( outputImageFilePath ) ,
107+ FailureReason = ImageFileOptimizationFailureReason . Ok
108+ } ;
109+ }
110+ catch
103111 {
104- OptimizedImageFile = new FileInfo ( outputImageFilePath ) ,
105- FailureReason = ImageFileOptimizationFailureReason . Ok
106- } ;
112+ image . Dispose ( ) ;
113+ throw ;
114+ }
107115 }
108116
109117 /// <summary>
0 commit comments