1818
1919// ReSharper disable CognitiveComplexity
2020// ReSharper disable PossibleNullReferenceException
21-
2221// ReSharper disable UnusedParameter.Local
23-
2422// ReSharper disable PossibleMultipleEnumeration
2523// ReSharper disable AssignNullToNotNullAttribute
26-
2724// ReSharper disable LoopCanBeConvertedToQuery
28-
2925// ReSharper disable InconsistentNaming
30-
3126// ReSharper disable UnusedMember.Global
3227
3328namespace SmartImage . Lib . Utilities
@@ -150,22 +145,21 @@ public static Dictionary<string, string> UtilitiesMap
150145
151146 #endregion
152147
153-
154148 public static Image GetImage ( string s )
155149 {
156- // TODO: Using Image objects creates a memory leak; disposing them doesn't fix anything either (?)
157-
158150 using var wc = new WebClient ( ) ;
159151
160- byte [ ] buf = wc . DownloadData ( s ) ;
152+ // byte[] buf = wc.DownloadData(s);
161153
162- var stream = new MemoryStream ( buf ) ;
154+ //using var stream = new MemoryStream(buf);
155+ //Debug.WriteLine($"Alloc {buf.Length}");
163156
164- Debug . WriteLine ( $ "Alloc { buf . Length } " ) ;
157+ using var stream = wc . OpenRead ( s ) ;
165158
166159 var image = Image . FromStream ( stream ) ;
167160
168161 return image ;
162+
169163 }
170164
171165
@@ -176,13 +170,17 @@ public static Image GetImage(string s)
176170 /// <param name="directType">Which criterion to use to determine whether a URI is a direct image </param>
177171 /// <param name="count">Number of direct images to return</param>
178172 /// <param name="pingTimeSec"></param>
179- /// <param name="readImage">Whether to read image metadata</param>
180- /// <param name="imageFilter">Filter criteria for images (applicable iff <paramref name="readImage"/> is <c>true</c>)</param>
181- public static List < DirectImage > FindDirectImages ( string url , DirectImageType directType = DirectImageType . Regex ,
182- int count = 5 , double pingTimeSec = 1 , bool readImage = true ,
183- Predicate < Image > imageFilter = null )
173+ public static List < string > FindDirectImages ( string url , DirectImageType directType = DirectImageType . Regex ,
174+ int count = 5 , double pingTimeSec = 1 )
184175 {
185- var images = new List < DirectImage > ( ) ;
176+ /*
177+ * TODO
178+ *
179+ * This function creates an insane memory leak.
180+ * Disposing neither the images nor the streams does anything (?)
181+ */
182+
183+ var images = new List < string > ( ) ;
186184
187185 string gallerydl = UtilitiesMap [ GALLERY_DL_EXE ] ;
188186
@@ -206,21 +204,15 @@ public static List<DirectImage> FindDirectImages(string url, DirectImageType dir
206204
207205 var standardOutput = output . StandardOutput ;
208206
207+
209208 while ( ! standardOutput . EndOfStream ) {
210209 string str = standardOutput . ReadLine ( )
211210 . Split ( '|' )
212211 . First ( ) ;
212+ if ( ! string . IsNullOrWhiteSpace ( str ) ) {
213+ images . Add ( str ) ;
213214
214- var di = new DirectImage
215- {
216- Direct = new Uri ( str ) ,
217- } ;
218-
219- if ( readImage ) {
220- di . Image = GetImage ( str ) ;
221215 }
222-
223- images . Add ( di ) ;
224216 }
225217
226218 var standardError = output . StandardError ;
@@ -239,8 +231,6 @@ public static List<DirectImage> FindDirectImages(string url, DirectImageType dir
239231
240232 manual :
241233
242- imageFilter ??= ( x ) => true ;
243-
244234 var pingTime = TimeSpan . FromSeconds ( pingTimeSec ) ;
245235
246236 IHtmlDocument document ;
@@ -295,40 +285,13 @@ public static List<DirectImage> FindDirectImages(string url, DirectImageType dir
295285 if ( ! IsDirect ( currentUrl , directType ) )
296286 return ;
297287
298- var di = new DirectImage
299- {
300- Direct = new Uri ( currentUrl )
301- } ;
302-
303-
304- bool isValid = ! readImage ;
305-
306- if ( readImage ) {
307- try {
308- var img = GetImage ( currentUrl ) ;
309-
310- isValid = imageFilter ( img ) ;
311-
312- if ( isValid ) {
313- di . Image = img ;
314-
315- }
316- else {
317- img . Dispose ( ) ;
318- }
319- }
320- catch ( Exception ) {
321- isValid = false ;
322- }
323- }
324288
325289 if ( imagesCopy . Count >= count ) {
326290 return ;
327291 }
328292
329- if ( isValid ) {
330- imagesCopy . Add ( di ) ;
331- }
293+ imagesCopy . Add ( currentUrl ) ;
294+
332295 } ) ;
333296
334297
@@ -372,9 +335,6 @@ public static List<DirectImage> FindDirectImages(string url, DirectImageType dir
372335
373336 ret :
374337
375- if ( readImage ) {
376- images = images . OrderByDescending ( x => x . Image . Width * x . Image . Height ) . ToList ( ) ;
377- }
378338
379339 return images ;
380340
@@ -391,23 +351,7 @@ internal static string AsPercent(this float n)
391351 }
392352 }
393353
394- public struct DirectImage : IDisposable
395- {
396- public Uri Direct { get ; internal set ; }
397-
398- [ CanBeNull ]
399- public Image Image { get ; internal set ; }
400-
401- public override string ToString ( )
402- {
403- return $ "{ Direct } { Image ? . Width } x{ Image ? . Height } ";
404- }
405-
406- public void Dispose ( )
407- {
408- Image ? . Dispose ( ) ;
409- }
410- }
354+
411355
412356 public enum DirectImageType
413357 {
0 commit comments