Those two versions are not the same. In "sync" version you obtain a reference to memory location of an image via image.GetPixelMemoryGroup()
. Then you read data from sourceStream
directly into that location.
In "async" version you again obtain reference to memory location via image.GetPixelMemoryGroup
but then you do something different - you call ToArray
. This extension method copies bytes from image memory location into new array, the one you hold in bytes
variable. You then read data from sourceStream
into that bytes
array, NOT directly into image memory locaiton. Then you discard bytes
array, so you read them to nowhere basically.
Now,MemoryMappedViewStream
inherits from UnmanagedMemoryStream
and all read\write operations are implemented in UnmanagedMemoryStream
. This kind of stream represents data in memory and there is nothing async it can do. The only reason it even has ReadAsync
is because base stream class (Stream
) has those methods. Even if you manage to make ReadAsync
work - in this case it will not be asynchornous anyway. As far as I know - MemoryMappedViewStream
does now allow real asynchronous access, even though it could make sense, since it has underlying file.
In short - I'd just continue with sync version, because there is no benefit in this case to use "async" one. Static analyzer of course doesn't know that, it only sees that there is Async-named analog of the method you use.