1111using System . IO ;
1212using System . Threading . Tasks ;
1313using DotNetCampus . Cli ;
14+ using DotNetCampus . MediaConverter . SkiaWmfRenderer . Optimizations ;
1415using DotNetCampus . MediaConverters . CommandLineHandlers ;
1516using SixLabors . ImageSharp . Formats . Png ;
1617using ErrorCode = DotNetCampus . MediaConverters . Contexts . MediaConverterErrorCode ;
@@ -25,19 +26,60 @@ static async Task<int> Main(string[] args)
2526 {
2627 // 调试模式
2728 var inputFile = "image.png" ;
29+
30+ if ( ! File . Exists ( inputFile ) )
31+ {
32+ inputFile = "image.wmf" ;
33+ }
34+
2835 if ( args . Length == 1 )
2936 {
3037 inputFile = args [ 0 ] ;
3138 }
3239
40+ if ( ! File . Exists ( inputFile ) )
41+ {
42+ Console . WriteLine ( $ "Input file not found. '{ Path . GetFullPath ( inputFile ) } '") ;
43+ return - 1 ;
44+ }
45+
46+ var testFolder =
47+ Directory . CreateDirectory ( Path . Join ( AppContext . BaseDirectory , $ "Test_{ Path . GetRandomFileName ( ) } ") ) ;
48+
49+ if ( string . Equals ( Path . GetExtension ( inputFile ) , ".wmf" ) )
50+ {
51+ Console . WriteLine ( $ "Input File is wmf file") ;
52+
53+ var wmfWorkingFolder = Path . Join ( testFolder . FullName , "WMF" ) ;
54+
55+ var optimizationContext = new EnhancedGraphicsMetafileOptimizationContext ( )
56+ {
57+ ImageFile = new FileInfo ( inputFile ) ,
58+ WorkingFolder = Directory . CreateDirectory ( wmfWorkingFolder ) ,
59+ MaxImageWidth = null ,
60+ MaxImageHeight = null ,
61+ ShouldLogToFile = true ,
62+ ShouldLogToConsole = true ,
63+ } ;
64+ EnhancedGraphicsMetafileOptimizationResult optimizationResult = EnhancedGraphicsMetafileOptimization . ConvertWmfOrEmfToPngFile ( optimizationContext ) ;
65+ Console . WriteLine ( $ "Finish convert to png file.{ optimizationResult } ") ;
66+
67+ if ( optimizationResult . IsSuccess )
68+ {
69+ inputFile = optimizationResult . OptimizedImageFile . FullName ;
70+ }
71+ else
72+ {
73+ return - 1 ;
74+ }
75+ }
76+
3377 var imageConvertContext = new ImageConvertContext ( )
3478 {
3579 MaxImageWidth = 1000 ,
3680 MaxImageHeight = 1000 ,
3781 } ;
38-
39- var testFolder =
40- Directory . CreateDirectory ( Path . Join ( AppContext . BaseDirectory , $ "Test_{ Path . GetRandomFileName ( ) } ") ) ;
82+
4183 var jsonText = imageConvertContext . ToJsonText ( ) ;
4284 var configurationFile = Path . Join ( testFolder . FullName , "image-convert.json" ) ;
4385 await File . WriteAllTextAsync ( configurationFile , jsonText ) ;
0 commit comments