@@ -2,6 +2,8 @@ namespace CoenM.ImageHash
2
2
{
3
3
using System ;
4
4
using System . IO ;
5
+ using System . Threading ;
6
+ using System . Threading . Tasks ;
5
7
using SixLabors . ImageSharp ;
6
8
using SixLabors . ImageSharp . PixelFormats ;
7
9
@@ -31,5 +33,28 @@ public static ulong Hash(this IImageHash hashImplementation, Stream stream)
31
33
using var image = Image . Load < Rgba32 > ( stream ) ;
32
34
return hashImplementation . Hash ( image ) ;
33
35
}
36
+
37
+ /// <summary>Asynchronously calculate the hash of the image (stream) using the hashImplementation.</summary>
38
+ /// <param name="hashImplementation">HashImplementation to calculate the hash.</param>
39
+ /// <param name="stream">Stream should 'contain' raw image data.</param>
40
+ /// <param name="cancellationToken">Cancellation token.</param>
41
+ /// <returns>hash value.</returns>
42
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="hashImplementation"/> or <paramref name="stream"/> is <c>null</c>.</exception>
43
+ /// <exception cref="SixLabors.ImageSharp.UnknownImageFormatException">Thrown when stream content cannot be loaded as an image.</exception>
44
+ public static async Task < ulong > HashAsync ( this IImageHash hashImplementation , Stream stream , CancellationToken cancellationToken )
45
+ {
46
+ if ( hashImplementation == null )
47
+ {
48
+ throw new ArgumentNullException ( nameof ( hashImplementation ) ) ;
49
+ }
50
+
51
+ if ( stream == null )
52
+ {
53
+ throw new ArgumentNullException ( nameof ( stream ) ) ;
54
+ }
55
+
56
+ using var image = await Image . LoadAsync < Rgba32 > ( stream , cancellationToken ) ;
57
+ return hashImplementation . Hash ( image ) ;
58
+ }
34
59
}
35
60
}
0 commit comments