1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Drawing ;
4+ using System . IO ;
5+ using System . Linq ;
16using NUnit . Framework ;
27using ServiceStack ;
38using ServiceStack . Testing ;
49using ExampleDataApis . ServiceInterface ;
510using ExampleDataApis . ServiceModel ;
11+ using ServiceStack . Text ;
612
713namespace ExampleDataApis . Tests ;
814
@@ -28,4 +34,64 @@ public void Can_call_MyServices()
2834
2935 Assert . That ( response . Result , Is . EqualTo ( "Hello, World!" ) ) ;
3036 }
37+
38+ [ Test ]
39+ public void Extract_Xkcd_Image_Dimensions ( )
40+ {
41+ var allLines = "xkcd-metadata.jsonl"
42+ . ReadAllText ( ) . Split ( "\n " ) ;
43+ using var jsonConfig = JsConfig . With ( new Config
44+ {
45+ TextCase = TextCase . SnakeCase
46+ } ) ;
47+
48+ var comics = allLines
49+ . Where ( x => ! x . IsNullOrEmpty ( ) )
50+ . Select ( JsonSerializer . DeserializeFromString < XkcdComic > )
51+ . ToList ( ) ;
52+
53+ var results = new List < XkcdComicDimensions > ( ) ;
54+ foreach ( var comic in comics )
55+ {
56+ try
57+ {
58+ var image = comic . ImageUrl . GetBytesFromUrl ( ) ;
59+ using var imageStream = new MemoryStream ( image ) ;
60+ var pngImage = Image . FromStream ( imageStream ) ;
61+ // Get the dimensions of the PNG image
62+ int width = pngImage . Width ;
63+ int height = pngImage . Height ;
64+ results . Add ( new XkcdComicDimensions
65+ {
66+ Id = comic . Id ,
67+ Width = width ,
68+ Height = height
69+ } ) ;
70+ // Dispose the image to release resources
71+ pngImage . Dispose ( ) ;
72+ }
73+ catch ( Exception e )
74+ {
75+ Console . WriteLine ( e ) ;
76+ }
77+ finally
78+ {
79+ results . Add ( new XkcdComicDimensions
80+ {
81+ Id = comic . Id ,
82+ Width = 0 ,
83+ Height = 0
84+ } ) ;
85+ }
86+ }
87+
88+ File . WriteAllText ( "xkcd-dimensions.json" , JsonSerializer . SerializeToString ( results ) ) ;
89+ }
90+ }
91+
92+ public class XkcdComicDimensions
93+ {
94+ public int Id { get ; set ; }
95+ public int Width { get ; set ; }
96+ public int Height { get ; set ; }
3197}
0 commit comments