1- using System ;
1+ using Oxage . Wmf ;
2+
3+ using SkiaSharp ;
4+
5+ using System ;
26using System . Collections . Generic ;
7+ using System . Diagnostics . CodeAnalysis ;
38using System . Linq ;
49using System . Text ;
510using System . Threading . Tasks ;
11+ using System . Xml ;
612
713namespace SkiaWmfRenderer ;
14+
815public static class SkiaWmfRenderHelper
916{
17+ public static bool TryConvertToPng ( FileInfo wmfFile , FileInfo outputPngFile )
18+ {
19+ if ( ! TryRender ( wmfFile , 0 , 0 , out var skBitmap ) )
20+ {
21+ return false ;
22+ }
23+
24+ using var outputStream = outputPngFile . OpenWrite ( ) ;
25+ skBitmap . Encode ( outputStream , SKEncodedImageFormat . Png , 100 ) ;
26+
27+ return true ;
28+ }
29+
30+ public static bool TryRender ( FileInfo wmfFile , int requestWidth , int requestHeight , [ NotNullWhen ( true ) ] out SKBitmap ? skBitmap )
31+ {
32+ skBitmap = null ;
33+
34+ using var fileStream = wmfFile . OpenRead ( ) ;
35+ var wmfDocument = new WmfDocument ( ) ;
36+ try
37+ {
38+ wmfDocument . Load ( fileStream ) ;
39+ }
40+ catch ( WmfException e )
41+ {
42+ return false ;
43+ }
44+
45+ var wmfRenderer = new WmfRenderer ( )
46+ {
47+ WmfDocument = wmfDocument ,
48+ RequestWidth = requestWidth ,
49+ RequestHeight = requestHeight ,
50+ } ;
51+
52+ return wmfRenderer . TryRender ( out skBitmap ) ;
53+ }
1054}
55+
56+ class WmfRenderer
57+ {
58+ public required WmfDocument WmfDocument { get ; init ; }
59+ public required int RequestWidth { get ; init ; }
60+ public required int RequestHeight { get ; init ; }
61+
62+ public bool TryRender ( [ NotNullWhen ( true ) ] out SKBitmap ? skBitmap )
63+ {
64+ skBitmap = null ;
65+
66+ Encoding . RegisterProvider ( CodePagesEncodingProvider . Instance ) ;
67+
68+ var format = WmfDocument . Format ;
69+
70+ var x = Math . Min ( format . Left , format . Right ) ;
71+ var y = Math . Min ( format . Top , format . Bottom ) ;
72+
73+ var width = Math . Abs ( format . Right - format . Left ) ;
74+ var height = Math . Abs ( format . Bottom - format . Top ) ;
75+
76+ return skBitmap == null ;
77+ }
78+ }
0 commit comments